R中的TclTk对象
一般来说,我对R和编程都是新手,所以如果其他地方已经回答了这个问题,我深表歉意。我无法通过搜索找到答案,但任何帮助或指导都会很好 我试图在R中创建一个可点击的界面,在这里我可以让用户点击找到一个选择的文件,然后在R中进行自动分析 以下是我遇到的问题:R中的TclTk对象,r,R,一般来说,我对R和编程都是新手,所以如果其他地方已经回答了这个问题,我深表歉意。我无法通过搜索找到答案,但任何帮助或指导都会很好 我试图在R中创建一个可点击的界面,在这里我可以让用户点击找到一个选择的文件,然后在R中进行自动分析 以下是我遇到的问题: require(tcltk) getfile <- function() {name <- tclvalue(tkgetOpenFile( filetypes = "{{CSV Files} {.csv}}")) if (na
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'
再次感谢您的帮助。谢谢 顶级对象有一个环境,您可以在其中存储东西,使它们位于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中。您仅在getfile函数中分配变量datafile。该函数存在后,该变量将不再存在。如果您使用数据文件
# 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)