0

I am trying to load data frames that are saved in a certain folder. I have a bash script that loops along all *.gzip files in the folder and passes the file names as arguments to an R script (blah.r $file_name). Now, in the R script, the file name is saved as

file_name = commandArgs(TRUE)[1]

and it prints the correct file name. However, when I try to load the file

data(file_name)

R thinks that file_name is a string, not a variable, so it says that it can't find "file_name".

How can I get R to recognize this variable as a variable and not a literal string?

3
  • This isn't a duplicate of the older question. That one was about accessing a variable given its name; this one is about reading a dataset from disk. Thus, get(*) isn't applicable here. Commented Oct 18, 2012 at 2:22
  • @HongOoi: great catch. Voted to reopen. Commented Oct 18, 2012 at 2:29
  • You don't load data into R using the data() function. That is for loading example data from installed packages. Commented Oct 18, 2012 at 7:47

1 Answer 1

2

The Answer to this the question asked is along the lines of

 read.table(file = file_name, ....)

where .... are additional arguments to specify the nature of the text file containing the data you wish to load.

If the data are not in a text file but in some other, possibly binary, format then you will need to use the correct function to import those data. For example, if the data are stored in one of R's formats (via save() or saveRDS()) then:

load(file = file_name) ## for save()-ed objects

foo <- readRDS(file = file_name) ## for objects serialised via saveRDS()

You mention the files are *.gzip. If so, then see ?connections and the gzip() function, which can also be passed an object containing the file name to open as in:

gzip(file_name, ....)

where .... again are further arguments you may need to specify (read the help page).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.