本文介绍了从终端安全地向 R 应用程序提供密码的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.html for an example of what I mean)

解决方案

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,"<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);
}

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

这篇关于从终端安全地向 R 应用程序提供密码的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-24 21:10