Set an argument of a function as the name of a global variable defined within a function in R

76 Views Asked by At

I would like to set a functions argument as the variable name of a global variable defined within a function. The reason is to create a general function which, connects to a database downloads data into a global variable with the argument of the function as its name.

This would allow to connect to a database once, download data, store them in a variable with a defined name and use it outside of the function. (Alternatively I am also open for different approaches)

test=function(name_argument){
substitute(name_argument)<<-2
}
test("name")
name->2 | The global variable should be callable with the variable name

I have tried using assign(), substitute(), eval() in various forms, without any success.

Does anyone a) know a solution to this and b) can describe the logic behind it. (For example why does substitute seemingly not work with global variables)

0

There are 0 best solutions below