knitr with bash: change working directory
You may use Rscript
to run a .Rmd
file and include any "R code" within the command line to keep your code chunks intact.
Rscript -e "library(knitr); opts_knit\$set(root.dir='~'); knit('test.Rmd')"
is an example bash command to run the test.Rmd
file below. You may change the root.dir
to suit your needs.
make directories```{r mkdir, engine='bash'}mkdir mytestmkdir mytest2```create one file in the 1st dir```{r create, engine='bash'}cd mytesttouch myfile```create another file in the 2nd dir```{r create2, engine='bash'}cd mytest2touch myfile2```check contents```{r ls, engine='bash'}ls mytest*```
The output:
```## mytest:## myfile#### mytest2:## myfile2```
Another way of looking at it would be to create every folder and file from where you are, and not move around with cd.
Create a directory tree
mkdir accepts more than one argument
mkdir -p 'plots/scatter' 'plots/box'# creates plots folder in the working directory,# and then creates scatter and box folders in it.
Create files in there
touch 'plots/scatter/firstfile.txt' 'plots/scatter/secondfile.txt'# single quotes mean literals
Keep key parts as variables
To easily change the folder structure from a one central variable:
scatter_folder=plots/scattertouch "$scatter_folder/third_file.txt" "$scatter_folder/fourth_file.txt"# double quotes allow for variable substitution.