The best option is to use the rstudioapi
package.
You should use the showPrompt()
function to get data that is able to be displayed; ie. is not confidential (usernames, etc).
Then, use the askForPassword()
function to get the confidential information (passwords, etc).
Word of warning here! You should never save passwords in the memory or as variables or as part of the global options. So be careful.
Nonetheless, the functionality still exists. So here's how you can do it:
# Get packages
library(rstudioapi)
# First, get the connection
conn <- showPrompt(title="Connection", message="Input connection:", default="xxx.xxx.xxx.xx")
# Second, get the port
port <- showPrompt(title="Port", message="Input port:", default="5119")
# Third, get the username
user <- showPrompt(title="Username", message="Input username:", default="")
# Fourth, get the password, anonymously
pass <- askForPassword(prompt="Input password:")
# Now, pull it all together
open_connection(host=host, port=port, user=paste0("<",user,">:<",pass,">"))
# Lastly, delete everything from memory
remove(conn, port, user, pass)
To be really risk-averse, it'd be better to place everything within a function. Because that way, the variables are definitely not saved to memory. You can implement it like this:
open_conn <- function(conn=NA, port=NA, user=NA, pass=NA) {
require(rstudioapi)
if (is.na(conn)) {
conn <- showPrompt(title="Connection", message="Input connection:", default="xxx.xxx.xxx.xx")
}
if (is.na(port)) {
port <- showPrompt(title="Port", message="Input port:", default="5119")
}
if (is.na(user)) {
user <- showPrompt(title="Username", message="Input username:", default="")
}
if (is.na(pass)) {
pass <- askForPassword(prompt="Input password:")
}
tryCatch(
{
open_connection(host=host, port=port, user=paste0("<",user,">:<",pass,">"))
},
error=function(cond) {
return(FALSE)
}
)
return(TRUE)
}
open_conn()
(Check this answer for properly formatting a tryCatch()
function)
Finally, I don't know what package you used for running the open_connection()
function... I assume it's from the the rkdb package (rkdb::open_connection()
), but since you didn't include the library()
command, I can't be sure.
If you wanted to see these functions in action, this is a good example of code where these functions have been implemented when automating certain git processes: get_Credentials()
.