Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output a vector in R in the same format used for inputting it into R

Tags:

Maybe I'm imagining this, but I think there is a built-in R function that lets you print an R vector (and possibly other objects like matrices and data frames) in the format that you would use to enter that object (returned as a string). E.g.,

> x <- c(1,2,3)
> x
[1] 1 2 3
> magical.function(x)
"c(1,2,3)" 

Does this function exist?

like image 965
John Horton Avatar asked Jun 02 '12 05:06

John Horton


People also ask

How do I convert a data type to an R vector?

Sometimes in processing data, it is necessary to convert other data structures into a r vector. When you need to convert a data type into an r vector so that you can apply vector functions to it, use the as.vector function.

What is a value vector in R?

Vectors in R are the same as the arrays in C language which are used to hold multiple data values of the same type. One major key point is that in R the indexing of the vector will start from ‘1’ and not from ‘0’. We can create numeric vectors and character vectors as well.

How to manipulate vectors in an R program?

R has many functions that can manipulate vectors or get more information about them. Here are some of the commonly used functions: 1. seq () – seq () function generates regular numeric sequences. The function has the following arguments: along.with: the length of this argument can define the length of the sequence.

What is the format function in R?

So now the part you have been waiting for – the examples! Definition: The format R function encodes data objects into common formats. Basic R Syntax: You can find the basic R programming syntax of the format function below. In the remaining post, I’ll show two examples for the application of the format function in R. So keep on reading…


2 Answers

dput maybe?

> test <- c(1,2,3)
> dput(test)
c(1, 2, 3)

You can also dump out multiple objects in one go to a file that is written in your working directory:

> test2 <- matrix(1:10,nrow=2)
> test2
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    3    5    7    9
[2,]    2    4    6    8   10
> dump(c("test","test2"))

dumpdata.r will then contain:

test <-
c(1, 2, 3)
test2 <-
structure(1:10, .Dim = c(2L, 5L))
like image 181
thelatemail Avatar answered Sep 19 '22 11:09

thelatemail


I decided to add this solution too because I found that dput() wasn't working for what I was trying to do. I have a shiny app that uses knitr to make reports based on the user session, and I use knit_expand() before rendering my .Rmd to port user parameters from the shiny session into the .Rmd.

Without going into too much detail, I have the need to port vectors "as is", because they will be written into lines of code in the .Rmd that someone will run. For this case, dput() didn't work because the output is only spit to the console, and the dump() method works but I didn't want to write new files every time and delete them.

There might be a better way, but I wrote a function that returns a character object of the vector "as is". It handles both numeric and character vectors (it throws quotes around each member of the character vector). It also handles single inputs and simply returns them as they are. It's not pretty, and I'm sure there are more efficient ways to write it, but it works perfectly for my needs. Thought I'd add this solution to the fray.

printVecAsis <- function(x) {
  ifelse(length(x) == 1, x, 
       ifelse(is.character(x), paste0("c(", paste(sapply(x, function(a) paste0("\'",a,"\'")), collapse=", "), ")"),
              paste0("c(", paste(x, collapse=", "), ")")))}
like image 36
ndimhypervol Avatar answered Sep 21 '22 11:09

ndimhypervol