Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to transform R code to string?

Tags:

r

I would like to transform c('1','2', 'text') into a character vector with only one elemenent c('1','2', 'text').

I have tried this:

> quote(c('1','2', 'text'))
c("1", "2", "text")

but

> class(quote(c('1','2', 'text')))
[1] "call"

and this:

> toString(quote(c('1','2', 'text')))
[1] "c, 1, 2, text"

which removes all the punctuation (while I would like to keep the exact same string).

like image 726
Dambo Avatar asked Sep 01 '25 04:09

Dambo


1 Answers

deparse is for converting expressions to character strings.

deparse(c('1','2', 'text'))
#[1] "c(\"1\", \"2\", \"text\")"

cat(deparse(c('1','2', 'text')))
#c("1", "2", "text")

gsub("\"", "'", deparse(c('1','2', 'text')))
#[1] "c('1', '2', 'text')"

deparse(quote(c('1','2', 'text')))
#[1] "c(\"1\", \"2\", \"text\")"

Also look at substitute

deparse(substitute(c(1L, 2L)))
#[1] "c(1L, 2L)"
like image 198
d.b Avatar answered Sep 02 '25 16:09

d.b