0

I am running this code in a R program, but In order to be able to use linux commands from the shell, I need to invoke sudo first. For instance, if I want to copy a file "cp file.tsv file_copy.txt" I have to write "sudo cp file.tsv file_copy.txt.Is it possible to use sudo in the code? How should I do that?

if (paste(dirname(inFileName), “/”, sep="") != OUTpath ) {
OS<- Sys.info()[“sysname”] 
if (OS==“Windows”) {
copyCommand<- “copy”
} else {
copyCommand<- “cp”
}

   paramsCopyFn<- sub(".tsv", "_copy.txt", paramsCopyFn, fixed=T)
   cmd1<- paste(copyCommand, shQuote(inFileName), shQuote(paramsCopyFn) , sep=" ")  
   execSystemCmd(cmd1)  
  }

execSystemCmd<- function(cmd, OS=NULL) {
  if (is.null(OS)) OS<-  Sys.info()["sysname"] 
  if (OS=="Windows") {
    shell(cmd, translate=TRUE)
  } else {
    system(cmd)
  }
gccd
  • 49
  • 2
  • 10
  • Can you run the code from a terminal with sudo directly ? – developer_hatch Feb 19 '18 at 17:19
  • I have tried copyCommand<- paste("sudo", "", "cp"), but I get the following error. sudo: no tty present and no askpass program specified s – gccd Feb 19 '18 at 19:44
  • See here: https://stackoverflow.com/a/24107529/2190251 – FelixEnescu Feb 19 '18 at 21:24
  • Thank you for your response. I have been reading about sudo and its security issues and I realized that I was using the wrong approach. One does not need to prefix the "cp" command with sudo. On the other hand, I can copy/rename a file in R using the functions copy() and rename() – gccd Feb 20 '18 at 02:28

1 Answers1

1

Open R as sudo using gksu or sudo /path/to/R/binary. It should be enough.

  • Thanks! I would like to try that. I am working with the RStudio IDE. I will try the command with the RStudio console. – gccd Feb 20 '18 at 18:54