我是R(和一般编程)的新手,所以如果这个问题在其他地方得到了回答,我深表歉意。我无法通过搜索找到答案,但任何帮助或方向都会很好!
我正在尝试在R中制作一个可点击的界面,在那里我可以让用户点击找到一个选择的文件,然后在R中进行自动分析。
下面是我遇到的问题:
require(tcltk)
getfile <- function() {name <- tclvalue(tkgetOpenFile(
filetypes = "{{CSV Files} {.csv}}"))
if (name == "") return;
datafile <- read.csv(name,header=T)
}
tt <- tktoplevel()
button.widget <- tkbutton(tt, text = "Select CSV File to be analyzed", command = getfile)
tkpack(button.widget)
# The content of the CSV file is placed in the variable 'datafile'然而,当我尝试执行它,并在按钮弹出后单击感兴趣的CSV文件时,什么也没有发生。我的意思是,当我输入数据文件时,R给了我下面的错误。
Error: object 'datafile' not found再一次,任何帮助都是非常感谢的。谢谢!
发布于 2014-10-18 17:00:03
顶级对象有一个环境,您可以在其中存储内容,并将它们保留在GUI本地。将该对象作为参数提供给回调函数:
# callback that reads a file and stores the DF in the env of the toplevel:
getfile <- function(tt) {name <- tclvalue(tkgetOpenFile(
filetypes = "{{CSV Files} {.csv}}"))
if (name == "") return;
tt$env$datafile <- read.csv(name,header=T)
}
# callback that does something to the data frame (should check for existence first!)
dosomething <- function(tt){
print(summary(tt$env$datafile))
}
# make a dialog with a load button and a do something button:
tt <- tktoplevel()
button.choose <- tkbutton(tt, text = "Select CSV File to be analyzed", command = function(){getfile(tt)})
tkpack(button.choose)
button.dosomething <- tkbutton(tt, text = "Do something", command = function(){dosomething(tt)})
tkpack(button.dosomething)这应该是相当健壮的,尽管我不确定环境中是否有任何东西是您应该小心践踏的,在这种情况下,在该环境中创建一个环境并将其用于本地存储。
如果要退出对话框并为用户保存内容,请提示输入名称,并在退出时使用assign将其存储在.GlobalEnv中。
https://stackoverflow.com/questions/26435916
复制相似问题