Way to securely give a password to R application from the terminal?

前端 未结 5 720
闹比i
闹比i 2020-11-29 04:24

Does R have a function that allows a user to provide a password securely, such as Python\'s getpass module?

(see http://docs.python.org/library/getpass.

5条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-29 05:19

    The problem is that R does not have functions to control the terminal it is running in (something like Rncurses); probably this is due to portability issues.
    Some time ago I was struggling with the same problem and I ended up with a function using 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,"",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);  
    }  
    

    Of course it won't work in non-GUI environments.

提交回复
热议问题