[英]TclTk object in R
我是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
再次感谢您的帮助。 谢谢!
顶级对象具有一个环境,您可以在其中存储内容,并将其保留在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)
尽管我不确定环境中是否已经存在任何值得您踩踏的东西,在这种情况下,它应该相当健壮,在这种情况下,请在该环境中创建一个环境并将其用于本地存储。
如果要退出对话框并为用户保存内容,请提示输入名称,并在退出时使用.GlobalEnv
assign
其存储在.GlobalEnv
中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.