How to Save() with a Particular Variable Name

How to save() with a particular variable name

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

save(list = this.csv.data$unique_tag, file=saved_file_name)

How to save() with a particular variable name

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

save(list = this.csv.data$unique_tag, file=saved_file_name)

Using save() on a variable name and value connected through assign() in R

I found a solution (in a thread I unfortunately overlooked previously)

https://stat.ethz.ch/pipermail/r-help/2011-September/289404.html
Thus :
do.call(save, list(variable_name, file=paste(variable_name, "rda", sep = ".")))

@PiotrZieliński @LarsArneJordanger

R user-defined save load functions | Passing variable names as arguments using deparse(substitute)

You need to do the changes in both save and load functions. Use list argument in save function to save the data with the same variable names as passed value.

getSave <- function(folder, rdata){
val <- deparse(substitute(rdata))

save(list = val,
file = paste0("./", deparse(substitute(folder)), "/", val, ".RData"))
}

getSave(SData, x)
getSave(SData, y)

To load the data, specify the environment as global since by default the values are loaded in the called environment. Since you are loading the data in a function by default the values are loaded inside the function only.

getLoad <- function(folder, rdata){
load(paste0("./", deparse(substitute(folder)), "/", deparse(substitute(rdata)), ".RData"),
envir = .GlobalEnv)
}

getLoad(SData, x)
getLoad(SData, y)

So the issue was not related to deparse, substitue but how save and load functions work inside a user defined function.

Saving a variable as a file name and importing a function from the saved variable name - Python

You could use importlib.

import importlib
file = input('Enter: ')
mymodule = importlib.import_module(file)

You can access variables and functions like mymodule.func1().
More information here

Save object using variable with object name

Try save(list=objectName, file=paste(objectName, '.Rdata', sep='') ).

The key is that the list argument to save takes a list of character strings that is the names of the objects to save (rather than the actual objects passed through ...).



Related Topics



Leave a reply



Submit