r - Transform function to a character string without losing comments -


i want transform functions character strings. can't directly transform class function class character, i'm using function list character logic. however, list removes comments functions.

example:

foo <- function() {     1 + 1 } # can't use as.character(foo), thus: as.character(list(foo))     "function () \n{\n    1 + 1\n}" 

however, if function contains comment gets removed

bar <- function() {     # sum     1 + 1 } as.character(list(bar))     "function () \n{\n    1 + 1\n}" 

question: why list removes comments functions , how save them? or better way transform functions character strings?

edit: suggestion mrflick use capture.output works interactive r session. however, if use code.r:

bar <- function() {     # sum     1 + 1 } # collectmetadata() print(paste0(capture.output(print(bar)), collapse="\n")) 

and try run rscript code.r output still: "function () \n{\n 1 + 1\n}"

i have no idea, why you'd want that. looks xy problem. anyway, seem want deparse function:

bar <- function() {   # sum   1 + 1 }  paste(deparse(bar, control = "usesource"), collapse = "\n") #[1] "function() {\n  # sum\n  1 + 1\n}" 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -