Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an easy way to run bash scripts on R Markdown, using Windows 10?

I've been learning R Markdown for the past months. It was fairly easy to get Anaconda Python to run within a R Markdown script, but I haven't yet found a way to get to run chunks of bash code within the same document.

Using

```{bash}
echo hello world
```

I get the error message

/bin/bash: C:\Users\MyName\AppData\Local\Temp\Rtmp0MpNfS\chunk-code-4cbc213a3545.txt: No such file or directory

Might any of you guys know what might be the root of this problem?

like image 395
mjbeyeler Avatar asked May 01 '18 13:05

mjbeyeler


People also ask

How do I run a bash script in Windows 10?

BASH will be available in the Command Prompt and PowerShell. Open Command Prompt and navigate to the folder where the script file is available. Type Bash script-filename.sh and hit the enter key. It will execute the script, and depending on the file, you should see an output.

How do I run a bash script in R?

Use Rscript to run R from bash You can run it using Rscript test. r . And even better, if you add an initial shebang line #!/usr/bin/env Rscript in the script above and make it executable with chmod +x test. r , you can directly launch your R script with ./test.

How do I run a bash program in Windows?

Press Windows key + X then click Command prompt, at the command prompt, type: bash then hit Enter. If you want to be able to access the local file system, press Windows key + X, Command Prompt (Admin) then type bash at the prompt.

What is the difference between R Markdown and Markdown?

R Markdown is an extension of the markdown syntax. R Markdown files are plain text files that typically have the file extension . Rmd . They are written using an extension of markdown syntax that enables R code to be embedded in them in a way which can later be executed.


1 Answers

I know maybe is to late but I had the same problem and I find the solution.

First, in Windows 8.1 or if you don´t have the Windows Subsystem for Linux (WSL) you can install Cygwin: https://cygwin.com/ > Install Cygwin

  • This is the recommendation pointed out in R Markdown: The Definitive Guide (https://bookdown.org/yihui/rmarkdown/) > 2.7 Other language engines > 2.7.2 Shell scripts

Second, add Cygwin to the PATH. Usually Cygwin is installed in C:\cygwin64 and the path to include will be in this case C:\cygwin64\bin

Third, check that everything is correct using base::Sys.which("bash") in R

  • If everything is correct it will appear something like this "C:\\cygwin64\\bin\\bash.exe"

Fourth, you can run in a Rmarkdown document, the chunk that you pointed out to double check:

```{bash}
echo hello world
```
like image 68
luifrancgom Avatar answered Oct 26 '22 12:10

luifrancgom