简体   繁体   English

R's deparse(替代(var))的对面?

[英]Opposite of R's deparse(substitute(var))?

I'm currently calling rp.slider from the tkrplot library with multiple arguments in a loop, for example: 我正在调用rp.slider库中的tkrplot ,在循环中有多个参数,例如:

rp.slider(rpplot, param1)
rp.slider(rpplot, param2)

etc. 等等

Ideally, I'd like to do this within a loop, eg 理想情况下,我想在循环中执行此操作,例如

for(i in 1:10) 
  rp.slider(rpplot, foo(paste(param,i,sep="")))

Where foo will encode the string to a variable name (symbol?). 其中foo将字符串编码为变量名(符号?)。 rp.slider converts the argument into a string using deparse(substitute(var)) . rp.slider使用deparse(substitute(var))将参数转换为字符串。 Is there a foo function that will let me do this? 是否有foo功能可以让我这样做? I've tried as.symbol , as.name , and parse (among others) without success. 我试过as.symbolas.name ,并parse (等等)没有成功。

Any help would be much appreciated! 任何帮助将非常感激!


To clarify, deparse(substitute(x)) returns [1] "x" - I'd like a way of returning the same output from a string, ie which foo outputs [1] "x" for input deparse(substitute(foo("x"))) ? 为了澄清, deparse(substitute(x))返回[1] "x" - 我想要一种从字符串返回相同输出的方法,即哪个foo输出[1] "x"用于输入deparse(substitute(foo("x"))) Is it possible? 可能吗?

Try eval(parse(text=...)) or eval(substitute(...)) . 尝试eval(parse(text=...))eval(substitute(...))

parse(text=...) turns the string in an expression, eval evaluates the expression. parse(text=...)表达式中的字符串, eval计算表达式。 Be sure to use the text argument, as parse normally looks for a file. 请务必使用text参数,因为parse通常会查找文件。 Forgetting that is a common mistake. 忘记这是一个常见的错误。 See also ?parse and ?eval . 另请参阅?parse?eval

> a <- 10
> x <- deparse(substitute(a))
> eval(parse(text=x))
[1] 10

To show how to use it, your adjusted code : 要显示如何使用它,您的调整后的代码:

for(i in 1:10)
  eval(parse(text=paste("rp.slider(rpplot,param",i,")",sep="")))

substitute substitutes values in a language object by the strings given in the second argument : substitute通过第二个参数中给出的字符串替换语言对象中的值:

for(i in 1:10)
  eval(
    substitute(
      rp.slider(rpplot,x),
      list(x=as.name(paste("param",i,sep="")))
    )
  )

Or, using the example in the help files : 或者,使用帮助文件中的示例:

library(rpanel)
rpplot <- rp.control(title = "Demonstration of rp.tkrplot", h = 1,j=1)

redraw <- function(panel) {
  rp.tkrreplot(panel, tkrp)
}
x <- c('h','j')
rp.tkrplot(rpplot, tkrp, function(panel) plot((1:20)^panel$j, (1:20)^panel$h))

eval(parse(text=paste("rp.slider(rpplot, ",x[1]," , action = redraw,
    from = 0.05, to = 2.00, resolution = 0.05)")))

eval(
  substitute(
    rp.slider(rpplot, x, action=redraw, from=0.05, to=2.00, resolution=0.05),
    list(x = as.name(x[2]))
  )
)

The explanation why this is necessary, can be found within the source code of rp.slider. 为什么这是必要的解释可以在rp.slider的源代码中找到。 The construct to get the varname inside the function is not the standard used in R. In fact, the use of 'deparse(substitute())' is strongly discouraged, exactly for this reason. 在函数中获取varname的构造不是R中使用的标准。事实上,强烈建议不要使用'deparse(substitute())',正是出于这个原因。 With most functions, as.expression("x") works to get the variable in using a variable name. 对于大多数函数, as.expression("x")用于使用变量名来获取变量。 Alas, the author of the rpanel package made this impossible. 唉, rpanel包的作者使这个变得不可能。

The rp.slider function looks like it is in rpanel, not tkrplot. rp.slider函数看起来像是在rpanel中,而不是tkrplot。

A possible alternative is to use the tkexamp function in the TeachingDemos package, it constructs a tk gui for a plot (using tkrplot) based on a list, you could construct the list in your loop, then call tkexamp. 一个可能的替代方案是在TeachingDemos包中使用tkexamp函数,它根据列表构建一个tk gui(使用tkrplot),你可以在循环中构造列表,然后调用tkexamp。 Or you can look at the code to tkexamp to see how it parses the list (in a loop) to create the tk controls, though looking through the code may scare you off from that idea. 或者您可以查看tkexamp的代码以查看它如何解析列表(在循环中)以创建tk控件,尽管查看代码可能会吓跑您的想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在R中向量化&#39;deparse(substitute(d))&#39;吗? - Vectorizing 'deparse(substitute(d))' in R? R:向量化解析(替代(x)) - R: Vectorize deparse(substitute(x)) 在没有 `eval` 的情况下颠覆外部函数的 `deparse(substitute())` - subvert external function's `deparse(substitute())` without `eval` 在lapply?deparse(substitute(x))? - deparse(substitute(x)) in lapply? deparse(substitute()) 与 lapply - deparse(substitute()) with lapply 对多个字符串使用deparse(substitute) - Using deparse(substitute) for several strings 如何使用assign(deparse(substitute(df)))将相同的函数应用于多个数据帧以覆盖输入变量? [R] - How to apply the same functions to multiple data frames to overwrite input variables using assign(deparse(substitute(df)))? [R] R 用户定义的保存加载函数| 使用 deparse(substitute) 将变量名作为参数传递 - R user-defined save load functions | Passing variable names as arguments using deparse(substitute) R:当函数调用为3点时,无法使用Deparse和Replace完全捕获所有参数 - R: cannot fully capture all arguments using deparse and substitute when function call is 3 dots 在R中,我如何定义一个等效于`deparse(substitute(x))`的函数? - In R, how do I define a function which is equivalent to `deparse(substitute(x))`?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM