22

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.

jbryer
  • 1,747
  • 3
  • 16
  • 29
  • 1
    This is not a general solution, but you could rely on `gksudo` (a graphical interface for sudo). – Jealie Apr 20 '14 at 20:02
  • 2
    I suggest taking a look at `man sudo`, the -A option may work for you. – James King Apr 20 '14 at 20:14
  • How are you running R? In my terminal I get prompted for my password if I need it when running sudo through `system` – Dason Apr 20 '14 at 20:17
  • I am running R through R Studio. I think that if I authenticated recently (say in the Terminal) then I do not get prompted for a password. Most of the time I do which is why this feature doesn't work. It is not critical, more for convenience. If anyone knows some other package that does something similar that would be helpful. I scanned devtools and installR (but this is really for Windows) and don't see anything helpful. – jbryer Apr 20 '14 at 20:28

3 Answers3

17

I can suggest two different solutions:

  1. 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:

      • It is secure, you don't have to handle the password yourself.
      • ....
    • CONS:

      • it won't work without a graphical interface.
      • gksudo was installed by default with linux brands I have tried, but YMMV: maybe some users won't have it.
      • ....
  2. 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:

      • It does not rely on any other program.
      • ....
    • CONS:

      • I don't like the idea that a password gets manipulated by 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.

Jealie
  • 6,157
  • 2
  • 33
  • 36
7

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"))
Gorka
  • 3,555
  • 1
  • 31
  • 37
1

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)
Salim B
  • 2,409
  • 21
  • 32