在R中读取用户输入(如密码)而不回显(适用于Windows操作系统)
与基于linux系统的工作解决方案相关的问题: 从上述问题的一个答案派生的以下代码在windows中无法正常工作。我希望此函数不会回显用户输入,但它正在回显用户输入:在R中读取用户输入(如密码)而不回显(适用于Windows操作系统),r,windows,echo,R,Windows,Echo,与基于linux系统的工作解决方案相关的问题: 从上述问题的一个答案派生的以下代码在windows中无法正常工作。我希望此函数不会回显用户输入,但它正在回显用户输入: get_password <- function() { cat("Password: ") #system("stty -echo") system("echo off") a <- readline() #system("stty echo") system("echo on") cat
get_password <- function() {
cat("Password: ")
#system("stty -echo")
system("echo off")
a <- readline()
#system("stty echo")
system("echo on")
cat("\n")
return(a)
}
get_password这篇由Markus Gesmann撰写的博文提供了我想要的解决方案:
以下函数getLoginDetails()
使用R包tcltk
和gWidgetstcltk
在弹出窗口中获取登录详细信息:
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)
getLoginDetails是另一个SO帖子,它创建了一个弹出对话框,您可以在其中输入密码。但是,我找不到隐藏明文的方法。谢谢@TimBiegeleisen,通过搜索与您展示的内容类似的内容,我找到了以下帮助我的博文:是的!这就是我记得以前见过的东西!真是一篇很棒的博客文章。您是否介意回答自己的问题,以便其他用户可以更轻松地找到解决方案?