18

Is there a way in R to enter passwords interactively to the command line and hide them from the screen? I know there's readLine, but I do not know passwords can be hidden there. Assume you want to connect to a database using ROracle or RMySQL and do not want to store the password in a script but rather make the user enter it every time a query is executed.

Yes, we had a similar question around here, but I feel that the situation has changed and it's worth to address to problem again. Unfortunately tcltk won't work on RStudio (Server).

However I have seen the folks at R Studio found a solution for their version control support. I remember previous versions where I could see my repo password every time I enter it in R Studio, but now that seems to be fixed. I know it's likely R Studio technique, but is there a way mortal R users can access it when working with R Studio Server?

Community
  • 1
  • 1
Matt Bannert
  • 27,631
  • 38
  • 141
  • 207
  • I suspect RStudio have a GUI widget in the RStudio code for repo passwords. Unlikely you can get to that from R code. And none of the other R GUI options are likely possible from RStudio Server. – Spacedman Jan 30 '13 at 12:21
  • @Spacedman don't want to cross-post, but do you suggest to rather post it to the R studio mailing list? Maybe they could point me to a Qt solution. – Matt Bannert Jan 30 '13 at 14:35
  • If you are on Windows, you can go one step further using PowerShell and the Windows Data Protection API to securely store encrypted passwords on disk, and then decrypt them when constructing your connecting string: http://stackoverflow.com/a/36218700/3827849 – Josh Gilfillan Apr 20 '16 at 23:03

1 Answers1

28

Actually R Studio (Server) provides a nice solution. You can access it by using .rs functions. They provide an undocumented password function which is really nice though there's no guaranteed support forever and a day:

.rs.askForPassword("foo")

You can find the the original hint from RStudio's Josh here: http://support.rstudio.org/help/discussions/questions/1448-password-interaction-with-dbs-on-rstudio-server

EDIT: As of 2015 there is another nice solution to enter password interactively. You could use shiny to have a little web based window with a password form. This discussion I had with @hadley shows an example snippet: https://github.com/rstats-db/RPostgres/issues/26

EDIT: As of 2017 there is another update to this. @m-dz pointed us to this: rstudioapi::askForPassword("Enter your pw") as well as getPass::getPass() from the R Studio Support webpage. Plus, I also realized that there is .rs.api.askForPassword() which is equivalent to the initially suggested call - at least as far as I've seen.

Matt Bannert
  • 27,631
  • 38
  • 141
  • 207
  • I have a dumb question. How do I use this function on Rstudio server? – Manoel Galdino Jun 07 '13 at 22:43
  • @ManoelGaldino: It just works like on Desktop R. I basically use it in database context like with ROracle or RPostgreSQL in order not to store DB passwords. Works for me on both Desktop and Server version. Did you encounter any specific trouble? – Matt Bannert Jun 10 '13 at 07:51
  • 7
    Was just browsing for this and on an [RStudio support webpage](https://support.rstudio.com/hc/en-us/community/posts/115000470088-function-rs-askForPassword-and-R-Markdown) `rstudioapi::askForPassword("")` is mentioned as a preferred function when using RStudio, also `getPass::getPass()` function is mentioned there which seems to be a more versatile solution. – m-dz Mar 06 '17 at 11:51
  • @m-dz: thanks for the update. I'll your comment to the answer. That wasn't around back in '13. – Matt Bannert Mar 06 '17 at 16:23
  • @MattBannert, exactly that was my intent! – m-dz Mar 06 '17 at 16:28