Question

I'm very new in R and I'm trying to build a GUI using Tcltk package. I'm not sure how the "tkGetOpenFile" works. I thought by using this function it would open and hold my dataset into the workspace of the RStudio. But the only thing that happens is a popup window to choose the file.

The code I'm using is below.

Please help me!!!

require(tcltk)
readCsv <- function(){
myval <- tkgetOpenFile()
mydata <- read.csv(paste(as.character(myval), collapse = " "))
assign("myData", mydata, envir = .GlobalEnv)
}

tt <- tktoplevel()
topMenu <- tkmenu(tt)           
tkconfigure(tt, menu = topMenu) 
fileMenu <- tkmenu(topMenu, tearoff = FALSE)
tkadd(fileMenu, "command", label = "Quit", command = function() tkdestroy(tt))
tkadd(fileMenu, "command", label = "Load", command = function() readCsv())
tkadd(topMenu, "cascade", label = "File", menu = fileMenu)
tkfocus(tt)
Was it helpful?

Solution

You need to use the tclvalue function to get an R character string representation of a Tcl variable. Modify your function as follows:

readCsv <- function(){
   myval <- tclvalue(tkgetOpenFile()) # add `tclvalue` here
   mydata <- read.csv(myval) # then `myval` is a character string
   assign("myData", mydata, envir = .GlobalEnv)
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top