Sometimes this works, sometimes not. It seems to depend on whether the system needs to prompt for a password. The more general question would be: is there a way for the user to provide input to a shell command from within R?
system('sudo npm install gitbook -g')
Note that my specific case is trying to install a node.js module. However, I think you can replicate it using a more trivial command.
system('sudo mkdir testdir')
Please note that this will sometimes work depending on whether sudo
requires you to re-enter a password. Thanks.
Type the relevant R function calls into the text file. To run an R command, put the cursor on the line of the command and then click the Run button at the top of the file window. Or just press CTRL-Enter.
To use a "root" terminal, type "sudo -i" at the command line. The entire group of default graphical configuration tools in Kubuntu already uses sudo, so you will be prompted for your password if needed using kdesu, which is a graphical frontend to sudo.
The sudo command allows you to run programs with the security privileges of another user (by default, as the superuser). It prompts you for your personal password and confirms your request to execute a command by checking a file, called sudoers , which the system administrator configures.
I can suggest two different solutions:
Use gksudo
, which will prompt the user for a password in a graphical interface. Here is how it works in practice:
system('gksudo ls')
PRO:
CONS:
gksudo
was installed by default with linux brands I have tried, but YMMV: maybe some users won't have it.Ask for the user password in R
, and supply it with the proper sudo
options: -k
to always ask for the password, and -S
to accept the password from the standard input. Here is how it works in practice:
system('sudo -kS ls',input=readline("Enter your password: "))
PRO:
CONS:
R
: it looks like a bad idea.Other than that, I am not aware on any way to interactively communicate with a program started from R
.
Just to build up on @Jealie's response. I believe 1. Won't work in new versions of ubuntu.
But we can let Rstudio handle the password:
system("sudo -kS ls", input = rstudioapi::askForPassword("sudo password"))
Since the gksudo
utility mentioned in @jealie's answer is not maintained anymore (and thus missing from Ubuntu 18.04 onwards), one has to rely on pkexec
instead as described here:
system("pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY ls")
The same command using R's newer system2()
function:
system2(command = "pkexec",
args = "env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY ls",
stdout = TRUE,
stderr = TRUE)
To run multiple commands in sequence with only a single password prompt, combine this with sudo
and bash -c '...'
:
system2(command = "pkexec",
args = paste0("env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY bash -c ",
"'sudo mkdir somedir; sudo ls -1l; sudo rm -R somedir'"),
stdout = TRUE,
stderr = TRUE)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With