简体   繁体   English

R重命名数据集

[英]R rename dataset

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. 我正在使用Tcltk软件包创建GUI,我想知道是否可以重命名要导入的数据集。 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. 我试图做的是与RStudio导入文件时相同的概念。 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). 尽管您可能希望将envir = .GlobalEnv其他名称(如果打算将其放置在CRAN上,则不能以这种方式分配给全局环境,而必须使用用户创建的环境)。

Some other notes: 其他注意事项:

  1. You probably want a tkdestroy call at the end of your onOK function. 您可能希望在onOK函数的末尾进行tkdestroy调用。
  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. 您可能想要添加逻辑以检查用户在按OK时是否确实为数据集提供了名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM