2010-05-26 159 views

回答

27

的問題是,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環境中工作。

+0

與廣告完美一致。謝謝! – A5C1D2H2I1M1N2O1R2T1 2013-08-01 04:01:18

+0

剛剛發現這在'getPass :: getPass()'函數中實現。 – 2017-03-06 12:25:37

5

終端安全密碼的問題非常簡單的Linux概念:

password <- function(prompt = "Password:"){ 
     cat(prompt) 
     pass <- system('stty -echo && read ff && stty echo && echo $ff && ff=""', 
         intern=TRUE) 
     cat('\n') 
     invisible(pass) 
    }   
+0

不適合我。薄荷18.2。 ''''>密碼() 密碼:stty:'標準輸入':設備的不當ioctl 警告消息: 正在運行命令'stty -echo && read ff && stty echo && echo $ ff && ff =「''had狀態1''' – Deleet 2017-12-14 20:53:49

1

我的包keyringr在Linux系統上的Windows,鑰匙扣從底層操作系統鑰匙圈取回密碼(DPAPI在OSX和侏儒匙扣解決了這個問題)。

vignette給出瞭如何使用該軟件包的詳細說明,但如果你使用OSX和有密碼保存在鑰匙串,你可以使用以下命令將密碼返回R(其中mydb_myuser是鑰匙扣項目名稱):

install.packages("keyringr") 
library("keyringr") 
mypwd <- decrypt_kc_pw("mydb_myuser") 
print(mypwd) 
+0

我得到'錯誤:Sys.info()[「sysname」] ==「Darwin」不是TRUE'。那是什麼意思?我應該按下「Alt-Right」嗎? – 2017-06-07 09:39:59

+0

你在使用什麼操作系統?請在github.com/jgilfillan/keyringr登錄問題。謝謝。 – 2017-06-07 09:45:17

+0

在Windows上,「Darwin」不是TRUE。 – 2017-06-07 13:36:39

0

這裏是一個登錄彈出,基於?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!" 
     } 
    }) 
    } 
) 
1

每在上述評論m-dz,有一個現在的封裝,這樣叫getPass,其中有一個功能單一,getPass()。這是base::readline()的替代品。

enter image description here

相關問題