在Windows上的RMarkdown中执行bash(Git-Bash)

时间:2017-09-22 13:29:17

标签: r windows bash rstudio knitr

我想在RStudio .rmd文件中执行bash语句,如下所示。

```{bash}
ls 
```

首先,RStudio会挂起,显示代码块旁边的红色按钮。我发现OpenSSH版本的bash正在打开一个shell窗口,所以通过调整windows系统路径,我得到了...\Git\bin\bash.exe版本来执行。

然后我收到红色的以下错误:

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

只有四个引擎(sh,bash,perl和python)中的每个引擎都可以在Rmarkdown页面上执行(在块的右边显示一个绿色三角形)。这比this page对Python,SQL,Bash,Rcpp,Stan,JavaScript和CSS描述的语言示例要少,而且比knitr::knit_engines对象中包含的38少得多。

在搜索StackOverflow时,我遇到了this short example,展示了如何编写自己的语言引擎。所以我修改它来访问Git-Bash shell,并按如下方式注册该函数:

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

但是当我执行chunk {r, engine='gitbash'}时,我收到了这个错误。

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

我已经阅读了很多Yihui Xie的优秀文档,但我似乎无法找到如何测试我编写的eng_gitbash函数。如果有人能告诉我“可以在命令行中直接使用选项$引擎来执行代码”,我或许可以让自己的版本工作。

同样作为一般性问题,有人可以指出我对语言引擎的环境要求的描述1)可被RStudio识别为可执行文件,2)使用Windows临时文件正确执行。

谢谢,苏

2 个答案:

答案 0 :(得分:1)

我能够让我的knitr语言引擎功能工作并“直接”执行它。这很简单:

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 )

它有点像Rmarkdown文件中的一种语言块。但正如您从下面的屏幕截图中看到的那样:

  1. 点击绿色小三角形执行。但是1输出是什么?
  2. 使用CTRL-SHIFT_ENTER执行。但是没有一点绿色三角形。
  3. 它不会执行,它们不是绿色三角形。

    [上面列出的3个块的屏幕截图] [1](唉,我不允许发布图像或超过两个链接,直到我有10个声望点)

    < / LI>

    我可以忍受这些问题。虽然,当我在RStudio的新测试版中尝试它时,使用CTRL_ENTER执行所有三个块工作正常。但是当我去Knit整个文件时,我得到了下面屏幕截图中显示的两个错误。

    1. 此错误是由直接在第一个块中执行eng_gitbash函数引起的。
    2. eng_gitbash函数似乎没有注册,即使我在编译文件之前我已经获得了一个R-Script,并且它在执行各个块时也有效。

      [上面列出的2个错误的屏幕截图] [2](唉,在我有10个声望点之前,我不允许发布图片或超过两个链接)

      < / LI>

      编织整个页面的设置与单个块的不同是什么?

答案 1 :(得分:0)

command = "top -bn 2 -d 0.01 | grep '^Cpu' | tail -n 1 | gawk '{print $2+$4+$6}'" 


The 1st iteration of top -b returns the percentages since boot, 
We need at least two iterations (-n 2) to get the current percentage. 
To speed things up, you can set the delay between iterations to 0.01. 
top splits CPU usage between user, system processes and nice processes, we want the sum of the three. 
Finally, you grep the line containing the CPU percentages and then use gawk to sum user, system and nice processes:

top -bn 2 -d 0.01 | grep '^Cpu' | tail -n 1 | gawk '{print $2+$4+$6}'
        -----  ------   -----------    ---------   ----------------------
        |      |           |             |             |------> add the values
        |      |           |             |--> keep only the 2nd iteration
        |      |           |----------------> keep only the CPU use lines
        |      |----------------------------> set the delay between runs
        |-----------------------------------> run twice in batch mode