shirokuma
shirokuma

Reputation: 1

How to use system() in R?

I want to invoke a system command to change directory in R console.

For example,

system(paste('"cd C:/Users/Dropbox/RPackages"'),intern = TRUE)
system(paste('"R CMD build test"'))
system(paste('"R CMD check test"'))
system(paste('"R CMD INSTALL test"'))
library(test)

These commands are supposed to run in Windows Command Prompt. But I want to bury them inside the RGUI script file to allow the change of parameters by users before wrap it into a Package.The error message I got from R console is

Error in system(paste("\"cd C:/Users/Dropbox/RPackages"\""), intern = TRUE) : 
'"cd C:/Users/Dropbox/RPackages"' not found

I also tried

system("cd C:/Users/Dropbox/RPackages",intern = TRUE)

but got similar error message

Error in system("cd C:/Users/Dropbox/RPackages", intern = TRUE) : 
'cd' not found

Upvotes: 0

Views: 6651

Answers (3)

ajsmith007
ajsmith007

Reputation: 106

Have you tried double back slashes?

'C:\\Users\\Dropbox\\RPackages'

Had a similar issue with R in Windows 7 using:

download.file(url=<url>, destfile='C:\\<dir>\\<dir>\\<file>')

Upvotes: 1

Spacedman
Spacedman

Reputation: 94202

system() runs each command in its own interpreter. Any change made to the working directory will not propagate.

If you want to do several things in a working directory, put all the commands in one system call, separated by a semi-colon (this works for Linux shells, not sure about Windows). Separating with \n also works in Linux, try that?

Compare these:

> getwd()
[1] "/nobackup/rowlings/Downloads/Dirs"
> system("cd Foo; pwd")
/nobackup/rowlings/Downloads/Dirs/Foo
> system("cd Foo") ; system(" pwd")
/nobackup/rowlings/Downloads/Dirs

Depending on your actual problem you might be better off using setwd() in R.

Upvotes: 2

Dirk is no longer here
Dirk is no longer here

Reputation: 368251

Why don't you use the R command setwd() to change directories -- see help(setwd) -- instead?

Upvotes: 8

Related Questions