Fortunately, I found some old code in a post by Barry Rowlingson that does just that. It uses the tcltk package in R to create a little window in which the user can enter her details, without showing the password. The tcltk package is part of base R, which means the code will run on any operating system. Nice!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
getLoginDetails <- function(){ | |
## Based on code by Barry Rowlingson | |
## http://r.789695.n4.nabble.com/tkentry-that-exits-after-RETURN-tt854721.html#none | |
require(tcltk) | |
tt <- tktoplevel() | |
tkwm.title(tt, "Get login details") | |
Name <- tclVar("Login ID") | |
Password <- tclVar("Password") | |
entry.Name <- tkentry(tt,width="20", textvariable=Name) | |
entry.Password <- tkentry(tt, width="20", show="*", | |
textvariable=Password) | |
tkgrid(tklabel(tt, text="Please enter your login details.")) | |
tkgrid(entry.Name) | |
tkgrid(entry.Password) | |
OnOK <- function() | |
{ | |
tkdestroy(tt) | |
} | |
OK.but <-tkbutton(tt,text=" OK ", command=OnOK) | |
tkbind(entry.Password, "<Return>", OnOK) | |
tkgrid(OK.but) | |
tkfocus(tt) | |
tkwait.window(tt) | |
invisible(c(loginID=tclvalue(Name), password=tclvalue(Password))) | |
} | |
credentials <- getLoginDetails() | |
## Do what needs to be done | |
## Delete credentials | |
rm(credentials) |
Session Info
R version 3.1.1 (2014-07-10)
Platform: x86_64-apple-darwin13.1.0 (64-bit)
locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8
attached base packages:
[1] tcltk stats graphics grDevices utils datasets methods base
0 Response to "Simple user interface in R to get login details"
Post a Comment