Reputation: 405
When I load my RDA file into the global environment, why does the resulting data frame change to a different name? How can I in rename it inside the function the RDA was created in?
My function is called SaveRDAtoDisk(table)
and takes in a table name, the function reads SQL data and saves a data frame as an RDA. The problem is I know that the table name that gets input has underscores, so I assign the desired output table name to a variable:
name.of.table.dt <- paste0(gsub("_", ".", table), ".dt", sep = "")
So this should output the desired data frame name.
The function SaveRDAtoDisk("big_table")
properly outputs an RDA called big.table.dt.rda
but when I load the RDA, it should be loaded with the name big.table.dt
, but it gets loaded as name.of.table.dt
which is the variable I assign it to in the function. What is going on how do I rename it?
# This function takes in a table name that contains underscores
# So I call SaveRDAtoDisk("log_hunter") and it loads a log.hunter.dt.rda into the current directory
# The problem is when I load the log.hunter.dt.rda file into the environment, the data frame is called name.of.table.dt (should be log.hunter.dt)
SaveRDAtoDisk <- function(table) {
# This line changes "log_hunter" into "log.hunter.dt.rda"
name.of.table.dt.rda <- paste0(gsub("_", ".", table), ".dt.rda", sep = "")
# This line changes "log_hunter" into "log.hunter.dt"
name.of.table.dt <- paste0(gsub("_", ".", table), ".dt", sep = "")
# This is the select statement that saves the sql query into the RDA file
name.of.table.dt.sql = paste0("select * from ", table, sep = "")
....
# Executes select query above
dt1 <- dbSendQuery(con, name.of.table.dt.sql)
# Should be "log.hunter.dt" <- fetch(dt1, rows.testing)
name.of.table.dt <- fetch(dt1, rows.testing)
# This properly saves the file "log.hunter.dt.rda"
save(name.of.table.dt, file = name.of.table.dt.rda)
dbClearResult(dt1)
}
So when I load the RDA into the environment, it loads a data frame called "name.of.table.dt" and not big.table.dt
even though the RDA is properly named and the variable is assigned in the function.
Upvotes: 1
Views: 1065
Reputation: 206243
save()
doesn't like to rename variables necessarily. You are asking it to save the name.of.table.dt
variable so that's exactly what it's doing. If you want to save this as some other variable, you need to create a variable with that name. Here's one way. You can create a temp environment with the object with the correct name, then use save in that environment.
table <- "hello" #testing
name.of.table.dt <- paste0(gsub("_", ".", table), ".dt", sep = "")
name.of.table.dt.rda <- paste0(gsub("_", ".", table), ".dt.rda", sep = "")
# store actual data in temp variable
# xx <- fetch(dt1, rows.testing)
xx <- 15 # testing
save(list=name.of.table.dt, file=name.of.table.dt.rda,
envir=as.environment(setNames(list(xx), name.of.table.dt)))
And then we can see that we can load "hello.dt"
exists("hello.dt")
# [1] FALSE
load(name.of.table.dt.rda)
exists("hello.dt")
# [1] TRUE
Upvotes: 2