R Markdown bash chunk cannot source ~/.bashrc

Hello everybody!

I have a problem with bash code chunks in RStudio Server on Linux Ubuntu 18.04.

Specifically, I would like the .bashrc to be sourced in each bash code chunk, both to define the $PATH and for running some scripts in the .bashrc file.

None of the following two options works for me:

  1. {bash engine.opts='-l'}

  2. source ~/.bashrc inside the bash chunk

Note that on Mac OS the latter works fine.

Using engine.opts='-i' does the job, but it also returns warnings that cannot be hidden with warnings=F:

bash: cannot set terminal process group (19193): Inappropriate ioctl for device
bash: no job control in this shell

As a last option, I now resourced to creating a text file called ~/rpath where I put my path definition and the scripts I need to be run. This file can actually be sourced inside the bash code chunk. However I would prefer a cleaner solution.
Can anybody help me with that?

all the best,
leonardo

There could be an open issue related to this in knitr

This worked for me while testing to load an environment variable define in one of my startup script

  • create a test.Rmd file with
---
title: "test bash option"
output: html_document
---

```{bash, engine.opts="-l"}
echo $BASHRC_LOADED
```

  • Defined BASHRC_LOADED in .bashrc or .bash_profile
export BASHRC_LOADED=TRUE

I got as result of knitr::knit("test.Rmd") in test.md

---
title: "test bash option"
output: html_document
---


```bash
echo $BASHRC_LOADED
```

```
## TRUE
```

Does this work for you ?

1 Like

Thanks again Christophe,

Actually that's the first thing I tried, but indeed it does not give the expected results.
I repeated exactly the code you gave me.
Interestingly, on Mac OS it does the job.

all the best,
leonardo

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

Yes you are right.
I just posted it here for referencing. If it is a known bug it is could to know and if someone want to contribute, it could be done there.

Thanks Christophe,

I had seen that before, but it does not solve the problem.

leonardo

1 Like

So it seems to work on MacOs and Centos. You should look into bash for ubuntu to see if there is some specificity about non interactive bash usage.

knitr will just execute a bash command of this type

bash <options> -c <code in chunk>

I don't know ubuntu well...

1 Like