I am trying to refer to a column of a data attribute of a spatial object using a string value of its name in R. I have no problem referring to the object only using eval(), but I could not refer to the attributes within the object using the same process.
So:
summary(eval(as.symbol(paste0("layerName", "_p", "5"))))
refers to the object layerName_p5 with no problems. However
summary(eval(as.symbol(paste0("layerName", "_p", "5", "@data$colName"))))
does not work, throwing the following error:
Error in summary(eval(as.symbol(paste0("layerName", "_p", "5", "@data$colName")))) : error in evaluating the argument 'object' in selecting a method for function 'summary': Error in eval(expr, envir, enclos) : object 'layerName_p5@data$colName' not found
I’ll note here that @data is used to point to the data attribute within the spatial object layerName_p5, and $colName is a column within that data attribute. I know this exists, because calling
summary(eval(as.symbol(paste0("opNeon", "_p", "5")))@data$patRoute)
does work. But I would ideally like to be able to refer to the second part of the call using strings as well.
So ultimately my question is why I cannot refer to attributes within an object using a string of its name with eval() (or get() either)?
Things I’ve tried include a nested eval() statement (which I may or may not have gone about using correctly…) and attempting to access attributes using square brackets. Both attempts with relevant error messages below:
eval(eval(as.symbol(paste0("layerName", "_p", "5"))),"@data$colName")
Error in eval(eval(as.symbol(paste0("layerName", "_p", "5"))), "@data$colname") : invalid 'envir' argument of type 'character'
eval(as.symbol(paste0("layerName", "_p", "5")))[eval(as.symbol("@data$colName"))]
Error in eval(expr, envir, enclos) : object '@data$colName' not found
I was wondering if anyone has come across this issue, and if yes if there are any solutions or if I'm just doing something wrong maybe? Any help would be very much appreciated!