0

I'm trying to run a script to check if a file or dataframe exists, and if it doesn't, to make a call to download the data and save the dataframe. The issue is that when the script tries to create the dataframe after the "else" statement in the code below, it either throws an error or doesn't name the dataframe properly.

Here's the full script:

> for (n in surveys) {
+   dataf <- sub("sg_(.+)","\\1d",n)
+   datafile <- sub("sg_(.+)","\\1d.Rda",n)
+   if ( exists( dataf ) == 1 | file.exists( datafile ) == 1) {print(paste("Skipped datapull   for ",dataf," because it already exists.")) } 

+ else {dataf <-    # There's the problem line
pullsg(eval(as.name(n)),sg_APIkey,sg_secret, verbose = TRUE, var_name_append = TRUE,);   # This pullsg() function seems to work fine)
+  save(eval(as.name(dataf)), file=datafile) }
+ }

For example, given n = "sg_student_assent", my desired outcome for the beginning of the "else" statement is this:

"student_assentd" <- pullsg(...)

But the actual outcome is this:

"dataf" <- pullsg(...)

When I try all the ways I know to have R return the value of the string assigned to dataf, they all give me errors. List below:

  1. as.name(dataf) <- pullsg(...)

  2. eval(as.name(dataf)) <- pullsg(...)

  3. deparse(substitute(dataf)) <- pullsg(...)

  4. substitute(dataf) <- pullsg(...)

All of them return a "could not find function" error, for example:

Error in deparse(substitute(dataf)) <- pullsg(eval(as.name(n)), sg_APIkey,  : 
  could not find function "deparse<-"

Error in as.name(dataf) <- pullsg(eval(as.name(n)), sg_APIkey, sg_secret,  : 
  could not find function "as.name<-"

Error in as.symbol(dataf) <- pullsg(eval(as.name(n)), sg_APIkey, sg_secret,  : 
  could not find function "as.symbol<-"

When I use the substitution directly instead of the "dataf" variable it returns this error:

Error in sub("sg_(.+)", "\\1d", n) <- pullsg(eval(as.name(n)), sg_APIkey,  : 
target of assignment expands to non-language object
Josh
  • 311
  • 3
  • 11
  • If you want to assign to a variable using a character string for its name, you would use `assign()`. Some variabtion of `assign(datag, pullsg(get(n)))` should work. But most of the time we try to avoid `eval()` and `assign()`. There are usually more R-like ways to do things. This would all work much better if you kept your data in a named list rather than separate variables in your global environment. – MrFlick Dec 23 '20 at 08:05
  • Thanks a lot @MrFlick, if I put my data in a named list, how do I call it? PS: It looks like I've got it working in following code (except it doesn't iterate): "for (x in surveys) { assign(paste0(x,".2"), mutate(assign(x, get(x)), r_teststatus = paste(urltest, istestdata, urlteststatus))) }" – Josh Jan 04 '21 at 17:02
  • 1
    If you have a list like `x<-list(a=1, b=2, c=3)` then you can get things out using strings like `x[["a"]]` or you can use the literal name `x$a`. You can also just apply functions over the list `lapply(x, sqrt)` – MrFlick Jan 04 '21 at 23:42

0 Answers0