首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R中的TclTk对象

R中的TclTk对象
EN

Stack Overflow用户
提问于 2014-10-18 11:06:20
回答 1查看 251关注 0票数 0

我是R(和一般编程)的新手,所以如果这个问题在其他地方得到了回答,我深表歉意。我无法通过搜索找到答案,但任何帮助或方向都会很好!

我正在尝试在R中制作一个可点击的界面,在那里我可以让用户点击找到一个选择的文件,然后在R中进行自动分析。

下面是我遇到的问题:

代码语言:javascript
复制
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给了我下面的错误。

代码语言:javascript
复制
Error: object 'datafile' not found

再一次,任何帮助都是非常感谢的。谢谢!

EN

回答 1

Stack Overflow用户

发布于 2014-10-18 17:00:03

顶级对象有一个环境,您可以在其中存储内容,并将它们保留在GUI本地。将该对象作为参数提供给回调函数:

代码语言:javascript
复制
# 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中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26435916

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档