11

I'm wondering if there is a way that I can read user input from STDIN without echoing it back to the screen in R. I know that readline(), readLines() and scan() can read in user input from the keyboard but none appear to have an option to not echo back.

As you might expect this is to grab a password. So I'm looking for something that would let me do:

> a<-get_password()
Password:
> a
[1] "password"
Thomas
  • 43,637
  • 12
  • 109
  • 140
David Lawrence Miller
  • 1,801
  • 11
  • 12
  • 3
    Can't be done cross-platformly easily, usual solution is tcltk: http://stackoverflow.com/questions/2917202 - but if you really need it from STDIN... – Spacedman Mar 01 '11 at 13:04
  • 1
    possible duplicate of [R: Way to securely give a password to R application?](http://stackoverflow.com/questions/2917202/r-way-to-securely-give-a-password-to-r-application) – Joris Meys Mar 01 '11 at 13:22

2 Answers2

10

What's the operating system? If you can run it from a terminal, this should work.

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

> a <- get_password()
Password: 
> a
[1] "sdfs"
> 

This works on OS X using R from Terminal.app, but not from R.app. No idea on a Windows solution, since there doesn't seem to be a native R solution.

Noah
  • 2,574
  • 1
  • 18
  • 12
5

If this is for a more production-style environment, then you might consider using R-Tcl/Tk, which has a "Show" parameter for password entry.

Ari B. Friedman
  • 71,271
  • 35
  • 175
  • 235