R编程语言有反射吗?
R有反射吗?
http://en.wikipedia.org/wiki/Reflection_(computer_programming)
基本上是我的想要做的是:
currentRun = "run287"
dataFrame$currentRun= someVar;
这样 dataFrame$currentRun
相当于 dataFrame$run287
。
这并没有阻止我解决任何问题,但从学术的角度来看,我想知道 R 是否支持反射编程。如果是这样,那么在给定的示例中如何使用反射呢? 谢谢你!
Does R have reflection?
http://en.wikipedia.org/wiki/Reflection_(computer_programming)
basically what i want to do is this:
currentRun = "run287"
dataFrame$currentRun= someVar;
such that dataFrame$currentRun
is equivalent to dataFrame$run287
.
This hasn't prevented me from solving any problems, but from an academic standpoint I would like to know if R supports reflective programming. If so, how would one go about using reflection in the example given?
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,R 支持反射编程。
这是该示例的 R 版本:
可能与
get
、assign
、eval
等相关。查看他们的在线帮助。yes, R supports reflective programming.
here is an R version of the example:
probably such as
get
,assign
,eval
is relevant. see online helps of them.在编程中应避免使用“$”运算符,因为它不会评估其参数,这与更通用的“[[”不同,
如果 myVar 的属性(特别是长度)正确,则“[[”将会成功。说 datFrame$currentRun 相当于 dataFrame$run287 是不正确的,但字符变量可以解释为列名是正确的。还有一个 eval(parse(text="...")) 构造,但最好尽可能避免。
The use of the "$" operator should be discouraged in programming because it does not evaluate its argument, unlike the more general "[["
Which will succeed if the properties (in particular length) of myVar are correct. It would not be correct to say the datFrame$currentRun is equivalent to dataFrame$run287, but is is correct that character variables can be interpreted as column names. There is also a eval(parse(text="...")) construct, but it is better to avoid if possible.
我不确定我是否完全掌握了维基百科文章,但是使用
[
建立索引是否能达到您想要的结果?一个简单的例子:或者
I'm not sure I fully grasped the wikipedia article, but does indexing with
[
achieve your desired result? A trivial example:or