开发者

Reading user input without echoing

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] "passw开发者_如何学编程ord"


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜