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:
- Create an object
x
with your string - Carefully escape all the special characters, in this case quotes and backslashes
- 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
Vary Colors of Axis Labels in R Based on Another Variable
How to Suppress Automatic Table Name and Number in an .Rmd File Using Xtable or Knitr::Kable
Matching Multiple Columns on Different Data Frames and Getting Other Column as Result
Extract Knots, Basis, Coefficients and Predictions for P-Splines in Adaptive Smooth
Run R Script from .Bat (Batch File)
How to Put the Labels Outside of Piechart
R Markdown: How to Make Text Float Around Figures
R: Split Variable Column into Multiple (Unbalanced) Columns by Comma
Number Formatting Axis Labels in Ggplot2
Check If R Is Running in Rstudio
How to Filter a Table's Row Based on an External Vector
Trying to Use Dplyr to Group_By and Apply Scale()
What Does the Double Percentage Sign (%%) Mean
Sort Data Frame Column by Factor