Bill Qualls
Bill Qualls

Reputation: 407

R -- I want data frame name to be determined by a command line argument

I want a data frame name to be determined by a command line argument. The following should make it clear what I was trying to do...I hope!

Execute using:

rterm --vanilla < c:/temp/myprog.txt --args XYZ

Contents of c:/temp/myprog.txt:

# I am using command line arguments
Args <- commandArgs(TRUE);

# Args[1] is the desired dataframe name
print(Args[1]);

# Create a simple dataframe
df <- c(c(1,2),c(3,4));
print(df);

# Save it
path <- 'c:/temp/mydata.rdata'
save(df, file=path);

# Clear the dataframe from memory
rm(df);

# Is it really gone?
print(df);

# Load the dataframe from disk
load(path);

# Did you get it?
print(df);

# --- This is where things start to go bad ---
# --- I know this is wrong, and I know why ---
# --- but it should make clear what it is  ---
# --- I am attempting to do.               ---

# Copy it to dataframe with name passed from command line
Args[1] <- df;

# Write it to disk with the new name
save(Args[1], file=path);

# Clear the dataframe from memory
rm(Args[1]);

# Is it really gone?
print(Args[1]);

# Load the dataframe from disk
load(path);

# Did you get it?
print(Args[1]);

# That's all

Thanks in advance.

** ADDED AFTER THE FACT...THIS WORKS...

C:\Program Files\R\R-2.14.2\bin\x64>rterm --vanilla < c:/temp/myprog.txt --args XYZ

> # I am using command line arguments
> Args <- commandArgs(TRUE);
>
> # Args[1] is the desired dataframe name
> print(Args[1]);
[1] "XYZ"
>
> # Create a simple dataframe
> df <- c(c(1,2),c(3,4));
> print(df);
[1] 1 2 3 4
>
> # Save it
> path <- 'c:/temp/mydata.rdata'
> save(df, file=path);
>
> # Clear dataframe so I can see if it
> # is really populated by the load
> rm(df);
>
> # Load the dataframe from disk
> load(path);
>
> # Did you get it?
> print(df);
[1] 1 2 3 4
>
> # --- This is where things start to go bad ---
> # --- I know this is wrong, and I know why ---
> # --- but it should make clear what it is  ---
> # --- I am attempting to do.               ---
>
> # Copy it to dataframe with name passed from command line
> assign(Args[1], df);
>
> # Write it to disk with the new name
> save(list=Args[1], file=path);
>
> # Clear memory so I can see if the dataframe
> # is really populated by the load
> rm(df);
> rm(XYZ);
>
> # Load the dataframe from disk
> load(path);
>
> # Did you get it?  Is its name as expected?
> # (In subsequent processing I will be able to
> # hard code the name as shown here.)
> print(XYZ);
[1] 1 2 3 4
>

Upvotes: 1

Views: 301

Answers (3)

mathematical.coffee
mathematical.coffee

Reputation: 56955

Try

assign(Args[1],df)

(See ?assign).

If Args[1] contains the string 'XYZ', then you will be able to refer to the dataframe by XYZ.

e.g.:

dfname <- 'XYZ' # your Args[1] I presume
df <- data.frame( a=runif(10) ) # random data frame

assign(dfname,df)

# now I can access df by typing XYZ:
XYZ
XYZ$a

# etc.

When you save, save(df,...) won't do the job - it'll save df with the variable name df.

Instead, you pass in the name of the variable you want to save using the list argument to save.

For example:

save(list='XYZ',file='tmp.rda')

When you then load('tmp.rda') you'll have the variable XYZ containing whatever it contained when you saved it.

So, for you:

# to show it works:
path <- 'tmp.rda'
save(list=Args[1],file=path)

rm(list=ls())
load(path)
print(XYZ) # this will work.

Upvotes: 2

Steve Lianoglou
Steve Lianoglou

Reputation: 7273

Would using the saveRDS and readRDS functions work instead of trying to jimmy your data.frame into a custom name?

Something like so:

x <- data.frame(a=1:10, b=letters[1:10])
saveRDS('some.file.rds')
rm(x)

XYZ <- readRDS('some.file.rds')
## Carry on ...

Upvotes: 0

Xu Wang
Xu Wang

Reputation: 10607

Instead of

Args[1] <- df;

try this:

assign(Args[1],df)

Upvotes: 0

Related Questions