1

Related question with working solution for linux based systems: Reading user input without echoing

The following code which is derived from one of the answers to above question is not working as expected for windows. I expected this function to not echo user input, but it is echoing user input:

get_password <- function() {
  cat("Password: ")
  #system("stty -echo")
  system("echo off")
  a <- readline()
  #system("stty echo")
  system("echo on")
  cat("\n")
  return(a)
}

I'm wondering if there is a way to read user input from R console while running an Rscript without actually showing it on the screen. Specifically I'm asking a solution that works in Windows OS.

Edit: I'm running my R 3.1.2 instance on Windows Server 2008 x64 and the above function is echoing the user input while it is asking for "Password: ".

Community
  • 1
  • 1
StrikeR
  • 1,598
  • 5
  • 18
  • 35
  • 1
    [Here](http://stackoverflow.com/questions/11311747/add-a-popup-text-box-within-an-r-script-using-tcltk) is another SO post which creates a popup dialog into which you could enter the password. However, I could not find a way to hide the clear text. – Tim Biegeleisen Feb 04 '16 at 05:45
  • 1
    Thanks @TimBiegeleisen , searching through stuff similar to what you have shown, I found the following blog post which helped me: http://www.magesblog.com/2014/07/simple-user-interface-in-r-to-get-login.html – StrikeR Feb 04 '16 at 07:18
  • Yes! This is what I thought I remembered seeing before! Truly a great blog post. Would you mind answering your own question so that other users might have an easier time finding a solution? – Tim Biegeleisen Feb 04 '16 at 07:20

1 Answers1

2

This blog post by Markus Gesmann has the solution of what I was looking for: Simple user interface in R to get login details

R Login pop-up

The following function getLoginDetails() uses the R packages tcltk and gWidgetstcltk to get the login details in a pop-up window:

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=" Login ", 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)
StrikeR
  • 1,598
  • 5
  • 18
  • 35