How to Save Data File into .Rdata

Saving dataframes as .Rdata files using a for loop

You need to make the name of your dataset outside of save() as it evaluates the first arguments as character or symbols and functions would not be evaluated.

Also, you need to get rid of quotations around the second paste0 and close your parentheses for save() which look like a typos.

for (i in 1:11){
dbname <- paste0("Dataset_",i)
save(dbname, file = paste0("Hypothesis1/Dataset",i, ".RData"))
}

Better approach would be using apply functions, listing your datasets name using ls, and passing them into save as character using list = ... argument.

lapply(ls(pattern="Dataset[0-9]+"), function(x) save(list = x, file = paste0("Hypothesis1/",x,".RData")))

Saving RData and RDS File Object from an Expression String

If you pass the object to be saved directly into the function this will work :

rdata_saver <- function(target="rdata-one",obj_name, rdata_name, directory = ''){
saveRDS(obj_name, paste0(directory, rdata_name, ".rds"))
}

rdata_saver("rdata-one", a, 'test', 'C:/Users/User/Downloads/')

If you are passing them as string we can use get.

rdata_saver <- function(target="rdata-one",obj_name, rdata_name, directory = ''){
saveRDS(get(obj_name), paste0(directory, rdata_name, ".rds"))
}

rdata_saver("rdata-one", 'a', 'test', 'C:/Users/User/Downloads/')

How to read data files, save and delete one by one in R

Try this:

csv2rdata <- function(filename) {
nm <- sub("\\.csv$", "", basename(filename))
assign(nm, data.table::fread(filename), envir = environment())
save(list = nm, file = paste0(dirname(filename), "/", nm, ".RData"), envir = environment())
}

dir("C:\\Data", pattern = "*.csv", full.names = TRUE) %>% lapply(csv2rdata)

How to save multiple R objects to a file with other data?

I am not sure the reason for embedding in a log file, but you have two options here with saving r objects.

Option1. Save several memory objects at once.

save(ggplot1object, dataframe2, dataframe3, file = "location.filename.Rdata")

Then you can

load("location.filename.RData") 

and all 3 objects will be loaded into memory.

Option 2. Create a List and save the list.

save(list(ggplot1object, dataframe2, dataframe3), "location.filename.Rdata")

Then you can

load("location.filename.Rdata")

and the single list item with the 3 different pieces will be loaded into the environment. These can be ggplot output items like p1, p2, etc... that represent different plot objects.

Save RData to different directory

Your file= argument is:

file.path("daily.usa.RData")

which returns:

[1] "daily.usa.RData"

You need to save desired filepath as an object:

filepath <- file.path("C:/project/data scrape")

and then do your save with

file = file.path(filepath, "daily.usa.RData")

which gives:

[1] "C:/project/data scrape/daily.usa.RData"

How to save data and load into Shiny app from R.Data File

You cannot yet make persistent data storage on shiny servers run on shinyapps.io. The solution is to write and read to a cloud drive like dropbox. See https://shiny.rstudio.com/articles/persistent-data-storage.html

edit: if you are just local or on your own server, you can use save() and load() for RData files.



Related Topics



Leave a reply



Submit