Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7615493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:39:25+00:00 2026-05-31T02:39:25+00:00

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

  • 0

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
>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T02:39:26+00:00Added an answer on May 31, 2026 at 2:39 am

    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.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to produce a line chart using Flot, but I want the data
I want make changes in big data.frame column names (names(mp)) with the help of
I'm having trouble rearranging the following data frame: set.seed(45) dat1 <- data.frame( name =
I want to filter rows from a data.frame based on a logical condition. Let's
I want to convert variables into factors using apply() : a <- data.frame(x1 =
How can I get a data frame's name from a list? Sure, get() gets
Suppose I have the following dataframe: df <- data.frame(BR.a=rnorm(10), BR.b=rnorm(10), BR.c=rnorm(10), USA.a=rnorm(10), USA.b =
I have the following question: I have data frame which looks like this. I
I have sample data like this df <- data.frame(name = rep(letters[1:7], each = 24),
I've got a data frame that I read from a file like this: name,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.