【问题标题】:Reading user input (like passwords) in R without echoing (for Windows OS) [duplicate]在 R 中读取用户输入(如密码)而不回显(对于 Windows 操作系统)[重复]
【发布时间】:2023-03-29 02:28:01
【问题描述】:

与基于 linux 的系统的工作解决方案相关的问题:Reading user input without echoing

源自上述问题的答案之一的以下代码在 Windows 中无法正常工作。我希望这个函数不会回显用户输入,但它正在回显用户输入:

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

我想知道是否有一种方法可以在运行 Rscript 时从 R 控制台读取用户输入,而无需实际显示在屏幕上。具体来说,我要的是一种适用于 Windows 操作系统的解决方案。

编辑: 我在 Windows Server 2008 x64 上运行我的 R 3.1.2 实例,上面的函数在请求“密码:”时回显用户输入。

【问题讨论】:

  • Here 是另一个 SO 帖子,它创建一个弹出对话框,您可以在其中输入密码。但是,我找不到隐藏明文的方法。
  • 感谢@TimBiegeleisen,搜索与您展示的内容相似的内容,我发现以下博客文章对我有帮助:magesblog.com/2014/07/…
  • 是的!这是我记得以前看过的!真是一篇很棒的博文。您介意回答您自己的问题,以便其他用户可以更轻松地找到解决方案吗?

标签: r windows echo


【解决方案1】:

Markus Gesmann 的这篇博文提供了我正在寻找的解决方案:Simple user interface in R to get login details

以下函数getLoginDetails() 使用R 包tcltkgWidgetstcltk 在弹出窗口中获取登录详细信息:

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)

【讨论】:

    猜你喜欢
    • 2016-01-04
    • 1970-01-01
    • 2016-03-26
    • 1970-01-01
    • 1970-01-01
    • 2016-03-03
    • 2016-09-27
    • 2018-07-20
    • 1970-01-01
    相关资源
    最近更新 更多