[R] pretty formatting of lists

Thomas Petzoldt thpe at simecol.de
Sun Mar 16 16:41:05 CET 2008


jim holtman wrote:
> Is this basically what you want?
>
>> xx <-list(aa = round(rnorm(30),2), f = function(a) a + b, a=x, b=x)
>> xx
> $aa
>  [1]  1.34 -0.21 -0.18 -0.10  0.71 -0.07 -0.04 -0.68 -0.32  0.06 -0.59
>  0.53 -1.52  0.31 -1.54 -0.30
> [17] -0.53 -0.65 -0.06 -1.91  1.18 -1.66 -0.46 -1.12 -0.75  2.09  0.02
> -1.29 -1.64  0.45
>
> $f
> function(a) a + b
>
> $a
> $a$a
> [1] -0.06 -0.16 -1.47
>
> $a$b
> [1] -0.48  0.42  1.36
>
>
> $b
> $b$a
> [1] -0.06 -0.16 -1.47
>
> $b$b
> [1] -0.48  0.42  1.36
>
>

No, I want a neat source code representation, not simply the printed
content -- technically something like the following, but more reader
friendly:

> cat(deparse(xx), fill=60)

structure(list(aa = c(0.25, 0.18, 0.84, -1.25, 0.09, -0.99, 1.64,
1.42, -1.29, -0.14, -1.07, -1.05, 0.98, 0.33, 1.76, -1.66, 0.96,
-0.21, -1.29, 0.78, -0.4, -1.63, -0.78, -1.05, 1.27, -1.44, 0.12,
-0.4, 0.02, 1.03), f = function (a)
a + b, structure(list(a = c(2.22, 0.36, 0.74), b = c(0.46, 0.41,
1.46)), .Names = c("a", "b")), structure(list(a = c(2.22, 0.36,
0.74), b = c(0.46, 0.41, 1.46)), .Names = c("a", "b"))), .Names =
c("aa",  "f", "", ""))


I wonder whether a function for some kind of "pretty deparsing" already
exists before I start to write my own solution.

Thomas P.


> 
> On Sun, Mar 16, 2008 at 8:47 AM, Thomas Petzoldt <thpe at simecol.de> wrote:
>> Hello,
>>
>> is there already a function in any R package which does
>> source code formatting of deparsed lists?
>>
>> Let's create the following list:
>>
>> x <- list(a = round(rnorm(3), 2),
>>           b = round(rnorm(3), 2))
>>
>> xx <-c(aa = round(rnorm(30)), f = function(a) a + b, list(x, x))
>>
>>
>> Now, I want deparse it in a way that yields something like:
>>
>>
>> list(
>>   aa = c(0.25, 0.18, 0.84, -1.25, 0.09, -0.99, 1.64,
>>     1.42, -1.29, -0.14, -1.07, -1.05, 0.98, 0.33,
>>     1.76, -1.66, 0.96, -0.21, -1.29, 0.78, -0.4,
>>     -1.63, -0.78, -1.05, 1.27, -1.44, 0.12, -0.4,
>>     0.02, 1.03),
>>   f = function (a) a + b,
>>   list(
>>     a = c(2.22, 0.36, 0.74),
>>     b = c(0.46, 0.41, 1.46)
>>   ),
>>   list(
>>     a = c(2.22, 0.36, 0.74),
>>     b = c(0.46, 0.41, 1.46)
>>   )
>> )
>>
>>
>> Thanks a lot!
>>
>> Thomas P.
>>



More information about the R-help mailing list