0

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?

Community
  • 1
  • 1
Forever
  • 385
  • 3
  • 16

1 Answers1

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.

maRtin
  • 6,336
  • 11
  • 43
  • 66
  • 1
    How is `paste0` necessary here? – Roman Luštrik May 29 '17 at 07:40
  • Thanks for your answer. In `source activate ncl_stable` it gaves `sh: 2: source: not found` Then it executes the rest of the code but an error occurs: `fatal:NclGRIB2: Invalid Product Definition Template.` `Segmentation fault (core dumped)` When I execute this code on Linux Bash, it gave a list of nor recognized formats like this `ERROR 4: /vsimem/work.jpc not recognized as a supported file format.` `dec_jpeg2000: Unable to open JPEG2000 image within GRIB file.` But the script runs and convert the file, in R don't. – Forever May 29 '17 at 10:35
  • I tried some combinations, and the problem is in the first line, when executing `source activate ncl_stable` and the error: `sh: 2: source: not found` There is a lot of information... im looking what's the problem now. – Forever May 29 '17 at 10:51
  • 1
    Sorry, but I can't really help you with this. It might be better adressed in a separate new question. – maRtin May 29 '17 at 11:09
  • 1
    I'm sorry I have mislead you. It was a rhetorical question stating that `paste0` is not really necessary. As it is, it might give someone with little experience the feeling that it's needed, while it's really not. – Roman Luštrik May 29 '17 at 13:22
  • @RomanLuštrik sorry for the harsh response :) I am used to wrap my strings in paste statements because I usually concatenate them with variables. But you are right, in this case it might confuse people :) – maRtin May 29 '17 at 13:37