31

I am repeatedly applying a function to read and process a bunch of csv files. Each time it runs, the function creates a data frame (this.csv.data) and uses save() to write it to a .RData file with a unique name. Problem is, later when I read these .RData files using load(), the loaded variable names are not unique, because each one loads with the name this.csv.data....

I'd like to save them with unique tags so that they come out properly named when I load() them. I've created the following code to illustrate .

this.csv.data = list(data=c(1:9), unique_tag = "some_unique_tag")
assign(this.csv.data$unique_tag,this.csv.data$data)
# I want to save the data, 
# with variable name of <unique_tag>, 
# at a file named <unique_tag>.dat
saved_file_name <- paste(this.csv.data$unique_tag,"RData",sep=".")
save(get(this.csv.data$unique_tag), saved_file_name)

but the last line returns:

"Error in save(get(this_unique_tag), file = data_tag) : 
  object ‘get(this_unique_tag)’ not found"

even though the following returns the data just fine:

get(this.csv.data$unique_tag)
2
  • I think you could also take a look at the envir argument in load() to retain the original column names if they are important. If you don't need to process multiple files at one time, couldn't you rm() the object after your analysis before moving on to the next file? Commented Jan 13, 2011 at 1:41
  • @Chase, the column names still seem to come out fine, it's the duplicated names of the created dataframes that was the issue. In the long run I anticipate rbinding these data frames into one big one and as you suggest it may be I don't need them more than one at a time. Commented Jan 13, 2011 at 3:24

5 Answers 5

35

Just name the arguments you use. With your code the following works fine:

save(list = this.csv.data$unique_tag, file=saved_file_name)
Sign up to request clarification or add additional context in comments.

3 Comments

It "works" in that it doesn't return an error, but when I load(saved_file_name) it creates a variable called "whattosave" instead of "unique_tag" After loading ten files, I would only have the latest "whattosave" instead of ten unique variables...
@jonw - sorry I was too inattentive. I edited my answer, hope it will really work!
@jonw; Hey,shouldn't you be giving darczig the answer check?
28

My preference is to avoid the name in the RData file on load:

obj = local(get(load('myfile.RData')))

This way you can load various RData files and name the objects whatever you want, or store them in a list etc.

1 Comment

That'd be nice to explain what local and get do.
16

You really should use saveRDS/readRDS to serialize your objects. save and load are for saving whole environments.

saveRDS(this.csv.data, saved_file_name)
# later
mydata <- readRDS(saved_file_name)

Comments

0

you can use

save.image("myfile.RData")

Comments

0

This worked for me:

env <- new.env()
env[[varname]] <- object_to_save
save(list=c(varname), envir=env, file='out.Rda')

You could probably do it without a new env (but I didn't try this):

.GlobalEnv[[varname]] <- object_to_save
save(list=c(varname), envir=.GlobalEnv, file='out.Rda')

You might even be able to remove the envir variable.

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.