سؤال

I'm creating a GUI using Tcltk package and I would like to know if there is a way to rename the dataset I'm importing. Basically, I would like the user to choose a name for the dataset he imports, but my code below is not giving me that. What Im trying to do is the same concept as RStudio does when importing a file. Can someone point me to the right direction?

Thank you in advance!

require(tcltk)
tt <- tktoplevel()
tkwm.title(tt, "Read Text Data")
dsName <- tclVar("Dataset")
entry.Name <-tkentry(tt,width="20",textvariable=dsName)
entry.box <- tklabel(tt,text="Please enter dataset name:")
tkgrid(entry.box, entry.Name)
tkgrid(tklabel(tt,text=""))

onOk <- function() 
{
  myval <- tclvalue(tkgetOpenFile())
  myData <<- read.table(myval, header=TRUE, sep=",", dec = ".")
} 

OK.but <- tkbutton(tt,text="  Import  ",command=onOk)
Qt.but <- tkbutton(tt,text="  Cancel  ",command=function()tkdestroy(tt))
tkgrid(OK.but, Qt.but)
tkfocus(tt)
هل كانت مفيدة؟

المحلول

Instead of this line:

myData <<- read.table(myval, header=TRUE, sep=",", dec = ".")

Try something like:

myData <- read.table(myval, header=TRUE, sep=",", dec = ".")
assign(tclvalue(dsName), myData, envir = .GlobalEnv)

Though you may want to make envir = .GlobalEnv something else (if you're planning to put this on CRAN, you can't assign to the global environment in this way and instead have to use a user-created environment).

Some other notes:

  1. You probably want a tkdestroy call at the end of your onOK function.
  2. You might want to add a logical to check whether the user has actually supplied a name for the dataset when they press OK.
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top