Run a Bash Script from an R Script

Run a bash script from an R script

The way to debug this is to use cat to test whether your character string has been escaped correctly. So:

  1. Create an object x with your string
  2. Carefully escape all the special characters, in this case quotes and backslashes
  3. Use cat(x) to inspect the resulting string.

For example:

x <- 'samtools view filename.bam | awk \'{OFS="\\t"; print ">"$1"\\n"$10}\' - > filename.fasta'

cat(x)
samtools view filename.bam | awk '{OFS="\t"; print ">"$1"\n"$10}' - > filename.fasta

If this gives the correct string, then you should be able to use

system(x)

how to run bash script from R

I have a bash script with name test.sh existed in the folder c:/test

You're using Windows, and bash scripts are meant for Unix/Linux. So that isn't going to work.

You have to install a bash shell to run bash scripts. You can try Cygwin for starters; see here for an example question on running bash scripts in Cygwin. Alternatively, if you're on Windows 10 you can install the bash for Windows subsystem. In the latter case, your Windows filesystem can be accessed via /mnt/<drive letter>. So the script c:\test\test.sh would be /mnt/c/test/test.sh.

How to run a .sh script from R on Windows?

seq is part of coreutils so your first check should be if your Cygwin install has coreutils installed. You can find out how to install new packages on Cygwin here.

bash under Cygwin will inherit the path from Windows and this doesn't include /usr/bin. To fix this, tell it to behave as if it is invoked at login

all_arguments <- c("-l", scriptPath, parameters)

Note that the filenames will need to be recognisable to Cygwin. See https://cygwin.com/cygwin-ug-net/using.html#cygdrive

Running multiple R scripts sequentially from shell in the same R session

You could write a wrapper script that calls each script in turn:

source("file1.R")
source("file2.R")

Call this source_files.R and then run Rscript source_files.R. Of course, with something this simple you can also just pass the statements on the command line:

Rscript -e 'source("file1.R"); source("file2.R")'


Related Topics



Leave a reply



Submit