4

I want to execute bash statements in an RStudio .rmd file as follows.

```{bash}
ls 
```

At first RStudio would just hang showing the red button beside the code chunk. I figured out that the OpenSSH version of bash was opening a shell window, so by adjusting the windows system path, I got the ...\Git\bin\bash.exe version to execute.

Then I got the following error in red:

bash: C:\Users\{me}\AppData\Local\Temp\RtmpmEF1jM\chunk-code3cb46c6027b3.: No such file or directory

This happens with each of the only four engines (sh, bash, perl and python) that appear to be executable on the Rmarkdown page (showing a little green triangle to the right of the chunk). This is less than the language examples described on this page for Python, SQL, Bash, Rcpp, Stan, JavaScript and CSS and much less than the 38 contained in the knitr::knit_engines object.

Searching StackOverflow, I came across this short example showing how to write your own language engine. So I modified it to access the Git-Bash shell, and registered the function as follows:

eng_gitbash <- function( options ) { ... } 
knitr::knit_engines$set( gitbash = eng_gitbash )

But then when I executed the chunk, {r, engine='gitbash'}, I got this error.

'"C:\Users\{me}\AppData\Local\Temp\RtmpmEF1jM\chunk-code3cb476f93db0."' is not recognized as an internal or external command, operable program or batch file.

I have read though a lot of Yihui Xie's excellent documentation, but I can't seem to find how to test the eng_gitbash function that I wrote. If someone could show me how the "options$engine can be directly used in command line to execute the code", I might be able to get my own version working.

Also as a general question, can someone point me to a description of the environmental requirements for a language engine 1)to be recognizable as executable by RStudio and 2)to be executed properly using Windows temp files.

Thanks, Sue

Sue Parker
  • 311
  • 1
  • 3
  • 9

2 Answers2

1

I was able to get my knitr language engine function to work and execute it "directly". It's pretty simple:

eng_gitbash <- function( options ) {

# create a temporary file
  f <- basename( tempfile( "gitbash", '.', paste('.', "sh", sep = '' ) ) )
  on.exit( unlink(f) )    # cleanup temp file on function exit
  writeLines( options$code, f )
  out <- ''

# if eval != FALSE compile/run the code, preserving output
  if (options$eval) {
    out <- system2( "gitbash.cmd", f )
  }

# spit back stuff to the user
  engine_output( options, options$code, out )
  }

knitr::knit_engines$set( gitbash = eng_gitbash )

And it sort of works as a type of language chunk in an Rmarkdown file. But as you can see from the screen capture below:

  1. It executes by clicking on the little green triangle. But what is the 1 output?
  2. It executes with CTRL-SHIFT_ENTER. But there is no little green triangle.
  3. It doesn't execute and their is not little green triangle.

    [ Screen capture of 3 chunks listed above ][1] (Alas, I'm not allowed to post images or more than two links until I have 10 reputation points)

I can live with those issues. Although, when I tried it in the new beta version of RStudio, executing all three chunks with CTRL_ENTER worked fine. But then when I went to Knit the entire file, I got the two errors shown in the next screen capture below.

  1. This error is caused by the execution of the eng_gitbash function directly in the first chunk.
  2. The eng_gitbash function doesn't seem to be registered, even though I sourced an R-Script before knitting the file, and it worked when executing the individual chunks.

    [ Screen capture of 2 errors listed above ][2] (Alas, I'm not allowed to post images or more than two links until I have 10 reputation points)

How is the setup for knitting the whole page different than for individual chunks?

Sue Parker
  • 311
  • 1
  • 3
  • 9
0
processing file: eng_gitbash.Rmd
  |............
  ordinary text without R code

  |......................
label: unnamed-chunk-l
Quitting from lines 4-8 (eng_gitbash.Rmd)
Error in eval(expr, envir, enclos) : attempt to apply non-function  Calls:
         <Anonymous> ... handle -> withCallingHandlers -> withVisible -> eval -> eval

Line 4 Error in eval(expr, envir, enclos) : attempt to apply non-function Calls:
         <Anonymous> ... handle -> withCallingHandlers -> withVisible -> eval -> eval


output file: eng_gitbash. knit.md

Output created: eng_gitbash.html
Warning messages:
1: In get_engi ne(optionsSengi ne) :
  Unknown language engine 'gitbash' (must be registered via knit_engines$set())

2: In get_engi ne(optionsSengi ne) :
  Unknown language engine 'gitbash' (must be registered via knit_engines$set()) 
Sue Parker
  • 311
  • 1
  • 3
  • 9