[R] Max characters from deparse(substitute(.))?
Spencer Graves
spencer.graves at structuremonitoring.com
Tue May 6 02:09:08 CEST 2014
On 5/5/2014 3:42 PM, William Dunlap wrote:
> Have you looked at the width.cutoff and nlines arguments to deparse?
> width.cutoff controls how long a line can be and it quits producing
> output (saving time and space) after nlines of output are produced.
> You want essentially the following
> f0 <- function(x, maxChar=20) deparse(x, width.cutoff=maxChar, nlines=1)
> or, to add the '...' to the end if something was cut off
> f1 <- function(x, maxChar=20) {
> # deparse warns and uses 65 if maxChar<20, so avoid that.
> retval <- deparse(x, width.cutoff=max(20, maxChar+1), nlines=1)
> if (nchar(retval) > maxChar) {
> retval <- paste0(substring(retval, 1, maxChar), "...")
> }
> retval
> }
Hi, Bill: Thanks very much. I could have solved it myself if I had
known which FMTR. Thanks again. Spencer
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com
>
>
> On Mon, May 5, 2014 at 2:44 PM, Spencer Graves
> <spencer.graves at structuremonitoring.com> wrote:
>> Is there a standard or or a standard utility to limit the size of
>> deparse(substitute(.))?
>>
>>
>> Below please find an example of the problem plus one solution.
>> If another solution already exists, I might prefer to use it.
>>
>>
>> Thanks,
>> Spencer
>>
>>
>> ##
>> ## Problem
>> ##
>> deparse.x0 <- function(x)deparse(substitute(x))
>> deparse.a <- do.call(deparse.x0, list(letters))
>> nchar(deparse.a) # unacceptable
>>
>> [1] 62 65 4
>>
>>
>> ##
>> ## Better
>> ##
>> deparse.x <- function(x, maxChar=20){
>> name.x <- deparse(substitute(x))
>> nch.x <- nchar(name.x)
>> name2 <- name.x[nch.x>0]
>> nch2 <- nch.x[nch.x>0]
>> if((length(name2)>1)){
>> name2 <- name2[1]
>> }
>> if(nch2[1]>maxChar){
>> name2 <- paste0(substring(name2, 1, maxChar), '...')
>> }
>> name2
>> }
>>
>> do.call(deparse.x, list(letters)) # better
>>
>> [1] "c(\"a\", \"b\", \"c\", \"d\"..."
>>
>>
>>
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list