我正在研究r面板包。现在如果我有一个使用radiogroup按钮的函数,并且如果我尝试从rpanel菜单中运行该函数,我会收到此错误:
Error in panel$intname : $ operator is invalid for atomic vectors
但是,如果我按照sé运行该功能,即不是从rpanel菜单内部运行,而是通过独立调用它,则不会出现上述错误。这是一个简单的例子。尝试2种方式(1)运行整个代码并单击Addition然后单击菜单中的Add(2)单独运行add函数并使用add()调用。前者导致上述错误而后者不导致。此外,我看到只有当我的面板中有rp.radiogroup时才出现此错误。
我在Why doesn't R allow $ operator on atomic vectors?看到了帖子,但我该如何解决我的问题?我的示例代码如下:
install.packages(c("rpanel","tkrplot"))
my.menu <- function(panel) {
library(rpanel,tkrplot)
if (panel$menu=="Add"){
add()
}
else
panel
}
main.panel <- rp.control(title = "Main Menu",size=c(200,150))
rp.menu(panel = main.panel, var = menu,
labels = list(list("Addition", "Add")),action = my.menu)
# function to do adddition
add <- function(){
my.draw <- function(panel) {
if(panel$vals=="numbers"){
val<-as.numeric(panel$nmbr1)+as.numeric(panel$nmbr2)
}
else if(panel$vals=="strings"){
val <- paste(as.character(panel$nmbr1), "and" ,as.character(panel$nmbr2))
}
plot(1:10, 1:10, type="n", xlab="", ylab="",
axes=FALSE, frame = TRUE)
text(5, 5, paste("Result: ", val),cex=1.4)
panel
}
my.redraw <- function(panel) {
rp.tkrreplot(panel, my.tkrplot)
panel
}
my.panel <- rp.control(title = "Addition")
rp.textentry(panel = my.panel, var = nmbr1,
labels = "First: ", action = my.redraw, initval="100")
rp.textentry(panel = my.panel, var = nmbr2,
labels = "Second:", action = my.redraw, initval="200")
rp.radiogroup(panel = my.panel, var = vals,
values = c("numbers", "strings"),
action = my.redraw, title = "Type")
rp.tkrplot(panel = my.panel, name = my.tkrplot, plotfun = my.draw)
}
答案 0 :(得分:1)
您可以使用$:Change
进行转义面板$瓦尔斯
为:
面板[&#34;瓦尔斯&#34;]