Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different results from deparse in R 3.4.4 and R 3.5

Tags:

r

deparse produces different results in R 3.4.4 and R 3.5. The NEWS suggests some defaults settings have changed but it is not clear to me how to ensure deparse produces the same output in R 3.4.4 and R 3.5

R 3.4.4

> deparse(list(dec = 4L, b = "a"), control = "keepNA")
[1] "list(dec = 4, b = \"a\")"

R 3.5

> deparse(list(dec = 4L, b = "a"), control = "keepNA")
[1] "list(4, \"a\")"

EDIT:

Thanks to helpful suggestions by @HongOoi and @akrun the closest thing to a solution that will ensure the same result in both R 3.4.4 and R 3.5 seems to be:

dctrl <- if (getRversion() > "3.4.4") c("keepNA", "niceNames") else "keepNA"
deparse(list(dec = 4L, b = "a"), control = dctrl)
like image 814
Vincent Avatar asked May 19 '18 06:05

Vincent


2 Answers

I haven't got R 3.5 installed, but per the NEWS file, you could try the showAttributes and/or niceNames arguments to deparse:

These functions gain a new control option "niceNames" (see .deparseOpts()), which when set (as by default) also uses the (tag = value) syntax for atomic vectors. On the other hand, without deparse options "showAttributes" and "niceNames", names are no longer shown also for lists. as.character(list( c (one = 1))) now includes the name, as as.character(list(list(one = 1))) has always done.

like image 174
Hong Ooi Avatar answered Nov 07 '22 18:11

Hong Ooi


We could use substitute in R 3.5 to get the same result as in R 3.4.4

deparse(substitute(list(dec = 4L, b = "a")), control = "keepNA")
#[1] "list(dec = 4, b = \"a\")"
like image 42
akrun Avatar answered Nov 07 '22 17:11

akrun