Search code examples
rterminalcredentialspassword-protection

Ask user crendentials when running R in Terminal


I am running an R script from Terminal (not RStudio) and at some point I need the user to write the username and password to access an API. For this, I am using a non-safe approach with the following code:

cat('Write username: ')
username<-readLines("stdin", n=1)
cat('Write password: ')
password<-readLines("stdin", n=1)

I have seen there is a nice implementation in RStudio using the rstudioapi package and the askForPassword function but since I am running my script from Termianl, I wonder if there is any alternative or more secure way to ask for this information


Solution

  • getPass is your best solution.

    It supports RStudio and fallsback to tcltk and other backends if missing.

    password <- getPass::getPass() is your call.