【发布时间】:2011-02-24 10:08:58
【问题描述】:
R 是否具有允许用户安全地提供密码的功能,例如 Python 的 getpass 模块?
(请参阅http://docs.python.org/library/getpass.html 了解我的意思的示例)
【问题讨论】:
标签: r input passwords password-protection
R 是否具有允许用户安全地提供密码的功能,例如 Python 的 getpass 模块?
(请参阅http://docs.python.org/library/getpass.html 了解我的意思的示例)
【问题讨论】:
标签: r input passwords password-protection
问题是 R 没有功能来控制它正在运行的终端(类似于Rncurses);这可能是由于便携性问题。
前段时间我在同样的问题上苦苦挣扎,最后我得到了一个使用 TclTk 的函数:
getPass<-function(){
require(tcltk);
wnd<-tktoplevel();tclVar("")->passVar;
#Label
tkgrid(tklabel(wnd,text="Enter password:"));
#Password box
tkgrid(tkentry(wnd,textvariable=passVar,show="*")->passBox);
#Hitting return will also submit password
tkbind(passBox,"<Return>",function() tkdestroy(wnd));
#OK button
tkgrid(tkbutton(wnd,text="OK",command=function() tkdestroy(wnd)));
#Wait for user to click OK
tkwait.window(wnd);
password<-tclvalue(passVar);
return(password);
}
当然它不会在非 GUI 环境中工作。
【讨论】:
getPass::getPass() 函数中实现的。
终端安全密码问题的非常简单的linux概念:
password <- function(prompt = "Password:"){
cat(prompt)
pass <- system('stty -echo && read ff && stty echo && echo $ff && ff=""',
intern=TRUE)
cat('\n')
invisible(pass)
}
【讨论】:
> password() Password:stty: 'standard input': Inappropriate ioctl for device Warning message: running command 'stty -echo && read ff && stty echo && echo $ff && ff=""' had status 1
【讨论】:
Error: Sys.info()["sysname"] == "Darwin" is not TRUE。那是什么意思?我应该按Alt-Right吗?
"Darwin" is not TRUE 在 Windows 上。
这是一个基于?modalDialog的登录弹出窗口。
library("shiny")
shinyApp(
ui <- basicPage(
actionButton("login", "Login"),
verbatimTextOutput("secrets")
),
server <- function(input, output, session) {
vals <- reactiveValues(authenticated=FALSE)
passwordModal <- function(message=NULL) {
modalDialog(
textInput("username", "Username", input$username),
passwordInput("password", "Password", input$password),
if (!is.null(message)) div(tags$b(message, style="color: red;")),
footer = tagList(
modalButton("Cancel"),
actionButton("authenticate", "OK")
)
)
}
observeEvent(input$login, {
showModal(passwordModal())
})
observeEvent(input$authenticate, {
vals$authenticated <- FALSE
if (!is.null(input$username) && nzchar(input$username) &&
!is.null(input$password) && nzchar(input$password)) {
removeModal()
if (input$password == "letmein") {
vals$authenticated <- TRUE
} else {
showModal(passwordModal(message="Incorrect password!"))
}
} else {
showModal(passwordModal(message="Please fill in your username and password"))
}
})
output$secrets <- renderText({
if (vals$authenticated) {
paste("Don't tell anyone, ", input$username, ", but...", sep="")
} else {
"I can't tell you that!"
}
})
}
)
【讨论】: