How to call linux terminal code from R script

2019-09-15 08:29发布

问题:

My question is:

How can I call a Linux Terminal inside a R script?

I know it can be a silly question... my R code is here:

download.file('https://some.dir,
              destfile = '/home/myfile.grb2',method='auto',quiet = FALSE,
              mode="wb", cacheOK = TRUE)

after the download, I had to convert that file using a code from terminal... but I need this to be automatic. The converter code is this:

source activate ncl_stable
cd /home
ncl_convert2nc myfile.grb2

Googling I saw that Linux Terminal uses C++. I know there is a package to run C++ codes in to R, Rcpp, but it works like the linux terminal?

回答1:

This will do the trick:

system(paste0("source activate ncl_stable;cd /home;ncl_convert2nc myfile.grb2"))

You can find more information here or here.