R编程语言有反射吗?

发布于 2024-11-24 20:35:02 字数 430 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

策马西风 2024-12-01 20:35:02

是的,R 支持反射编程。

这是该示例的 R 版本:

foo <- function()1

# without reflection
foo()

# with reflection
get("foo")()

可能与 getassigneval 等相关。查看他们的在线帮助。

yes, R supports reflective programming.

here is an R version of the example:

foo <- function()1

# without reflection
foo()

# with reflection
get("foo")()

probably such as get, assign, eval is relevant. see online helps of them.

陌路终见情 2024-12-01 20:35:02

在编程中应避免使用“$”运算符,因为它不会评估其参数,这与更通用的“[[”不同,

currentRun = "run287"
dataFrame[[currentRun]]= someVar   # and the";" is superflous

> dat <- data.frame(foo = rnorm(10), bar = rnorm(10))
>  myVar <- "bar2"
> bar2 <- 1:10
>  dat[[myVar]] <- bar2
> str(dat)
'data.frame':   10 obs. of  3 variables:
 $ foo : num  -1.43 1.7091 1.4351 -0.7104 -0.0651 ...
 $ bar : num  -0.641 -0.681 -2.033 0.501 -1.532 ...
 $ bar2: int  1 2 3 4 5 6 7 8 9 10

如果 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 "[["

currentRun = "run287"
dataFrame[[currentRun]]= someVar   # and the";" is superflous

> dat <- data.frame(foo = rnorm(10), bar = rnorm(10))
>  myVar <- "bar2"
> bar2 <- 1:10
>  dat[[myVar]] <- bar2
> str(dat)
'data.frame':   10 obs. of  3 variables:
 $ foo : num  -1.43 1.7091 1.4351 -0.7104 -0.0651 ...
 $ bar : num  -0.641 -0.681 -2.033 0.501 -1.532 ...
 $ bar2: int  1 2 3 4 5 6 7 8 9 10

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.

梨涡 2024-12-01 20:35:02

我不确定我是否完全掌握了维基百科文章,但是使用 [ 建立索引是否能达到您想要的结果?一个简单的例子:

> dat <- data.frame(foo = rnorm(10), bar = rnorm(10))
> myVar <- "bar"
> dat[ , myVar]
 [1]  1.354046574  0.551537607  0.779769817  0.546176894 -0.194116973  0.959749309
 [7] -1.560839187 -0.024423406 -2.487539955 -0.201201268

或者

> dat[ , myVar, drop = FALSE]
            bar
1   1.354046574
2   0.551537607
3   0.779769817
....

I'm not sure I fully grasped the wikipedia article, but does indexing with [ achieve your desired result? A trivial example:

> dat <- data.frame(foo = rnorm(10), bar = rnorm(10))
> myVar <- "bar"
> dat[ , myVar]
 [1]  1.354046574  0.551537607  0.779769817  0.546176894 -0.194116973  0.959749309
 [7] -1.560839187 -0.024423406 -2.487539955 -0.201201268

or

> dat[ , myVar, drop = FALSE]
            bar
1   1.354046574
2   0.551537607
3   0.779769817
....
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文