在字符串上使用eval()访问R中的对象属性 [英] using eval() on string to access object attributes in R

查看:209
本文介绍了在字符串上使用eval()访问R中的对象属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用R中名称的字符串值引用空间对象的数据属性的列。仅使用eval()引用对象没有问题,但是我不能引用

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"))))

毫无疑问地引用了对象layerName_p5。但是

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:


摘要错误(eval(as.symbol(paste0( layerName, _p, 5,
@ data $ colName )))):在
中评估参数'object'时出错,为函数'summary'选择方法:eval(expr,envir,
enclos)中的错误:对象'layerName_p5 @ data $ colName '未找到

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

在这里我会注意到@data用于指向空间对象layerName_p5中的data属性,以及$ colName是该数据属性内的一列。我知道这是存在的,因为调用

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.

所以最终我的问题是为什么我不能使用带有eval()(或get())名称的字符串来引用对象内的属性?

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)?

我尝试过的事情包括嵌套的eval()语句(我可能会或可能不会正确使用…)并尝试使用方括号访问属性。两次尝试都带有以下相关错误消息:

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")




eval(eval(as.symbol(paste0( layerName, _p, 5))),
@ data $ colname)中的错误:类型为'character'的'envir'参数无效/ p>

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"))]




eval(expr,envir,enclos)中的错误:找不到对象'@ 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!

推荐答案

在工作示例中使用名称,或者

Using the names in your working example, either

eval(call("$",
          eval(call("@",
                    as.symbol(paste0("opNeon", "_p", "5")),
                    as.symbol("data"))),
          as.symbol("patRoute")))

eval(call("@",
          as.symbol(paste0("opNeon", "_p", "5")),
          as.symbol("data")))[["patRoute"]]

会工作。

如果要匹配 data.frame 列的部分名称,则第一个选项是这样的。对于第二个选项,您需要将 exact = FALSE 作为选项添加到 [[

If you want to match partial names for the data.frame column, the first option works as such. For the second option, you would need to add exact = FALSE as an option to [[. For example,

eval(call("@",
          as.symbol(paste0("opNeon", "_p", "5")),
          as.symbol("data")))[["patR", exact=FALSE]]

将使用 patR 作为 patRoute <的缩写形式返回相同的结果/ code>,假设 opNeon_p5 @ data 中没有其他列名,以字符串 patR 。

would return the same result using "patR" as a short form of "patRoute", assuming there is no other column name in opNeon_p5@datastarting with the string "patR".

我想在您的用例中(或通常是非交互式使用)几乎没有必要进行部分匹配。在R提示符下键入?Extract 以获取有关部分匹配的更多信息。

I guess there is little to no need for partial matching in your use case (or non-interactive use in general). Type ?Extract in the R prompt for more information on partial matching.

现在让我们切换到较短的标识符 a b 和<$其余示例为c $ c> c eval(as.symbol( a @ b)) eval(as.symbol( a $ c)) eval(as.symbol( a @ b $ c))不起作用,因为符号aka名称指代变量,但`@``$`,因为运算符不属于变量名。

Now let's switch to shorter identifiers a, b and c for the remaining examples. The reason why eval(as.symbol("a@b")), eval(as.symbol("a$c")) or eval(as.symbol("a@b$c")) will not work is that symbols a.k.a. names refer to variables, but `@` and `$` as operators are not part of the variable name.

小字

另一个可行的选择是 eval(parse(text = a @ b $ c)),其中 text 可以通过任何方式构造。只要您确定文本的内容,这是安全的,但是不建议评估任意表达式。

Another option that would work is eval(parse(text="a@b$c")), where text can be constructed by whatever means. This is safe as long as you are sure about the contents of text, but evaluating arbitrary expressions is not advisable.

这篇关于在字符串上使用eval()访问R中的对象属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆