El-ad David Amir
El-ad David Amir

Reputation: 197

Start R console with an RDS file already loaded into a variable

I have an RDS file, for example /tmp/data.RDS. I would like to start R and already have that file loaded into a variable in the environment. I tried:

R -e "data <- readRDS('/tmp/data.RDS')"

However, the -e parameter exits the console when the command is done.

I also tried:

R --interactive -e "data <- readRDS('/tmp/data.RDS')"

Unfortunately, it looks like --interactive and -e are mutually exclusive -- the first one in the command line will be used, the other one ignored. In other words, this will not run the part after -e.

Thank you for reading!

Upvotes: 2

Views: 377

Answers (1)

Jealie
Jealie

Reputation: 6277

AFAIK you have two possibilities:

Solution 1. Customize your .Rprofile to capture the commandArgs() vector (see also nicola's comment). For example, put this in your .Rprofile:

if (any(commandArgs()=="load_rds")) {
  l = which(commandArgs()=="load_rds")  
  data = readRDS(commandArgs()[l+1])
}

and then start your session with: R --args load_rds a.rds

Solution 2. This solution is little bit more hacky, but does not require you to modify your .Rprofile. You can (a) start a non-interactive R session which loads the data and then ends, and then (b) start an interactive R session that loads your previous workspace. Just type in a console:

R --save -e 'data = readRDS("~/a.rds")'; R --restore

Does this help?

Upvotes: 1

Related Questions