【发布时间】:2016-11-14 15:12:55
【问题描述】:
我需要写一些关于使用 shell 的讲座。我已经完成了 100 个 Swaveved R 文档。当我看到 knitr 有 bash 引擎 (http://yihui.name/knitr/demo/engines) 时,我将它用于测试用例。
这工作正常,不是很好。块没有正在进行的会话,每个块评估都从文档的工作目录开始。之间没有意识 块,因此很难写出以连贯的方式使用“cd”和“pwd”。
我希望每个块都有一个参数“working.directory”。
如果您知道其他方法可以做到这一点,我会全力以赴。目前,我正在探索 Emacs org 模式作为替代方案。它有一种方法可以为每个块设置工作目录。但样式更难。
这是我的 knitr 测试文档,
---
title: "Command Line, Shell"
author:
- name: Paul Johnson
affiliation: Center for Research Methods and Data Analysis, University of Kansas
email: pauljohn@ku.edu
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 -->
<!-- ``` -->
【问题讨论】:
-
knitr 中的大多数非 R 会话都不是持久的,因此
cd不会通过以下 bash 块进行。请参阅github.com/yihui/runr 了解另一种可能性。 -
嗨@Yihui。我会尝试跑步者。您在 knitr 页面上对此的 cmet 不太热情。
-
抱歉打扰了。我找到了一个可行的答案并将其发布在下面。您能否告诉我们由于多次运行 opts_knitr 来更改 bash 块之间的 root.dir 是否会出现缺陷?