0

Is it possible to use the argument passed to a function to create an object name within the function? For example in the code below I'm trying to check the value of use_x and create a data table called "x_list":

use_x = "Y"
    myfun <- function(type_name){
   if (eval(parse(text= paste("use_",type_name, sep = "") )) == "Y"){
    type_name_list <- data.table(item = 1, name = "some name here", condition = "Y")}
}

myfun("x")
  • 7
    Generally this isn't such a great idea in R. I would recommend that you avoid [eval(parse=))](http://stackoverflow.com/questions/13649979/what-specifically-are-the-dangers-of-evalparse). And if you create a data.table in your function, it will only exist there unless you return it. And at that point you will need to give it a new name. I suggest you describe what you are really trying to do and we may be able to suggest a more R-like way to do things. – MrFlick Jun 09 '16 at 21:06
  • simplest solution is to create `environment` and operate on symbols as character variables within that environment, both for assign and get – jangorecki Jun 10 '16 at 00:33
  • I should probably clarify that I am trying to convert SAS code into R, which is why I'm having this problem (I'm not too familiar with SAS but it seems to be better equipped for this sort of thing) – user6447431 Jun 11 '16 at 02:33

0 Answers0