r - knitr 引擎=bash。想要为每个 block 单独设置工作目录

标签 r shell knitr

我需要写一些关于使用 shell 的讲座。我已经完成了数百份 Swaveved R 文档。当我看到 knitr 有 bash 引擎( http://yihui.name/knitr/demo/engines )时,我将其用于测试用例。

这工作正常,但不是很好。 block 没有正在进行的 session ,每个 block 评估都从文档的工作目录开始。之间没有意识 block ,因此很难以连贯的方式编写使用“cd”和“pwd”的内容。

我希望每个 block 都有一个参数“working.directory”。

如果您知道其他方法可以做到这一点,我洗耳恭听。目前,我正在探索 Emacs org 模式作为替代方案。它有一种方法来设置每个 block 的工作目录。但样式设计比较困难。

这是我的knitr测试文档,

---
title: "Command Line, Shell"
author:
 - name: Paul Johnson
   affiliation: Center for Research Methods and Data Analysis, University of Kansas
   email: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b6c6d7c3dadcd9ded8f6ddc398d3d2c3" rel="noreferrer noopener nofollow">[email protected]</a>
abstract: 
    This uses knitr to interleave shell commands and output.
Note to Authors: please_dont_change_the_next 4 lines!
date: "`r format(Sys.time(), '%Y %B %d')`"
output:
  html_document:
    highlight: haddock
---

```{r setup, include=FALSE}
outdir <- paste0("tmpout")
options("prompt" = "$ ")
options("continue" = "$ ")

if (!file.exists(outdir)) dir.create(outdir, recursive = TRUE)
knitr::opts_chunk$set(engine="bash", echo=TRUE, comment="output:",
prompt = TRUE, fig.path=paste0(outdir, "/p-", root.dir="/tmp"))
options(width = 70)
```

## Getting Started in the Shell

### Start a Terminal Emulator
   * Mac (Utilities/Terminal, iTerm, many others)

   * Windows (Git for Windows provides a BASH shell). Others available (Cygwin, Ubuntu BASH for Windows)

   * Linux (many terminal programs, some more exciting than others)

### High points
   * Type commands, see textual output

   * Not much pointing and clicking (copy/paste possible on some)

### What is a Shell?
   * Computer has many settings that programs can access. A "shell"
     is a "behind the scenes" program that keeps those settings
     and passes them out to programs when needed
   * BASH: Bourne Again Shell, a widely-used shell
   * Almost all Unix/Mac systems will have many shell programs
     available, such as "sh", "bash", "dash", and so forth. We
     are mostly interested in BASH because it has the most user comfort
   * A terminal program relies on the shell to translate between user and operating system

#### **Prompt**. Where you type 
   * Prompt might be verbose 
```
pauljohn:Documents/Projects $
```
   * On some it is very lean, just the dollar sign
```
$
```
   * Dollar sign is customary prompt for non-root user (non-administrators)
   * Prompt is configurable, it is a good exercise for somebody who has used the Terminal for a week or two on a daily basis.

## Basic things to type

#### What is my working directory
```{r getwd}
pwd
```

```{r ls}
ls
```
See there, the output says it is still in my working directory
where the document is, not "/tmp" as requested.

The following efforts produce errors if you uncomment them.

I can't figure even how to change directory with system or Sys.setenv.

<!-- ```{r whoami5} -->
<!-- system("cd /tmp") -->
<!-- ls -->
<!-- ``` -->



<!-- ```{r whoami6} -->
<!-- Sys.setenv(PWD = "/tmp") -->
<!-- ls -->
<!-- ``` -->

最佳答案

这是一个可行的解决方案。它没有实现持久的 BASH session ,但它实现了与 Emacs org-mode 相同的解决方法。它允许为各种代码 BASH block 单独设置工作目录。这几乎与拥有持久的 shell session 一样好。

如何操作?当需要更改工作目录时,在engine="R"代码块中使用knitr::opts_knitr来重置 block 之间的root.dir。以下 bash block 尊重新设置。

只有@Yihui可以告诉我们这是否会导致缓存等危险; R block 中有关于 setwd 的强烈警告。

这是一个有效的示例,首先从工作目录“/tmp”开始,然后是我的主目录“~”。

---
title: "Command Line, Shell"
author:
 - name: Paul Johnson
   affiliation: Center for Research Methods and Data Analysis, University of Kansas
   email: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="700011051c1a1f181e301b055e151405" rel="noreferrer noopener nofollow">[email protected]</a>
abstract: 
    This uses knitr to interleave shell commands and output.
Note to Authors: please_dont_change_the_next 4 lines!
date: "`r format(Sys.time(), '%Y %B %d')`"
output:
  html_document:
    highlight: haddock
---

```{r setup, include=FALSE}
outdir <- paste0("tmpout")
options("prompt" = "$ ")
options("continue" = "$ ")

if (!file.exists(outdir)) dir.create(outdir, recursive = TRUE)
knitr::opts_chunk$set(engine="bash", echo=TRUE, comment="output:",
prompt = TRUE, fig.path=paste0(outdir, "/p-"))
options(width = 70)
knitr::opts_knit$set(root.dir="/tmp")
```

## Basic things to type

#### What is my working directory. These are BASH chunks, not R chunks!
```{r getwd}
pwd
```

```{r ls}
ls
```

Here is how I change directory for following bash chunks.
Note engine = bash in this document, so we set R 
engine just for this one

```{r, engine = "R"}
opts_knit$set(root.dir="~")
```

```{r ls2}
ls
```      

我刚刚上传了 html 输出作为编译的证据:

http://pj.freefaculty.org/scraps/test_dummy.html

关于r - knitr 引擎=bash。想要为每个 block 单独设置工作目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40591989/

相关文章:

r - Mac 上 Rstudio 中的环境变量

linux - 每次通过 ftp 上传新文件时都使用相同的名称解压文件

r - 在 pdf 输出中的 kable 表中创建水平线

r - 使用 condformat 和 Knir 在 pdf 上创建表格?

linux - 从 ps -ef |grep 关键字获取 pid

r - 更改 Rstudio 中 kable 表的默认文本颜色

r - 将所有列中的所有值乘以与日期中的月份相对应的天数?

r - 遗传算法优化

r - 使用 Arima 模型预测不在序列结束之前的时期的值

shell - ansible playbook命令中的问题?