knitr引擎=bash。要为每个区块分别设置工作目录吗
我需要写一些关于使用外壳的讲座。我已经做了100多份SWEED R文件。当我看到knitr拥有bash引擎()时,我将其用于一个测试用例 这个效果不错,不是很好。区块没有正在进行的会话,每个区块评估都从文档的工作目录开始。他们之间没有意识 因此,很难以连贯的方式写出关于使用“cd”和“pwd”的文章 我希望每个区块都有一个参数“working.directory” 如果你知道其他方法,我洗耳恭听。目前,我正在探索Emacs组织模式作为替代方案。它可以为每个块设置工作目录。但它更难形成风格 这是我的knitr测试文件knitr引擎=bash。要为每个区块分别设置工作目录吗,r,shell,knitr,R,Shell,Knitr,我需要写一些关于使用外壳的讲座。我已经做了100多份SWEED R文件。当我看到knitr拥有bash引擎()时,我将其用于一个测试用例 这个效果不错,不是很好。区块没有正在进行的会话,每个区块评估都从文档的工作目录开始。他们之间没有意识 因此,很难以连贯的方式写出关于使用“cd”和“pwd”的文章 我希望每个区块都有一个参数“working.directory” 如果你知道其他方法,我洗耳恭听。目前,我正在探索Emacs组织模式作为替代方案。它可以为每个块设置工作目录。但它更难形成风格 这是我
---
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 -->
<!-- ``` -->
---
标题:“命令行,Shell”
作者:
-姓名:保罗·约翰逊
勘萨斯大学研究方法与数据分析中心
电邮:pauljohn@ku.edu
摘要:
这将使用knitr交错shell命令和输出。
作者注意:请不要更改接下来的4行!
日期:“`r格式(Sys.time(),'%Y%B%d')`”
输出:
html_文件:
推荐理由:黑线鳕
---
```{r设置,include=FALSE}
outdir这是一个有效的解决方案。它没有实现持久的BASH会话,但它实现了与Emacs组织模式相同的解决方法。它允许为各种代码BASH块分别设置工作目录。这几乎和拥有一个持久的shell会话一样好
怎么做?当需要更改工作目录时,使用engine=“R”代码块中的knitr::opts_knitr在块之间重置root.dir。下面的bash块尊重新设置
只有@Yihui可以告诉我们这是否会在缓存中造成危险等;在R块中有关于setwd的强烈警告
下面是一个有效的示例,首先从工作目录“/tmp”开始,然后是我的主目录“~”
---
标题:“命令行,Shell”
作者:
-姓名:保罗·约翰逊
勘萨斯大学研究方法与数据分析中心
电邮:pauljohn@ku.edu
摘要:
这将使用knitr交错shell命令和输出。
作者注意:请不要更改接下来的4行!
日期:“`r格式(Sys.time(),'%Y%B%d')`”
输出:
html_文件:
推荐理由:黑线鳕
---
```{r设置,include=FALSE}
outdir knitr中的大多数非R会话都不是持久的,因此cd
将不会执行以下bash块。请看另一种可能性。你好@Yihui。我试试runr。你在knitr页面上对它的评论不太热情。很抱歉让你分心。我找到了一个可行的答案,并把它贴在下面。能否请您告诉我们,由于在bash块之间多次运行opts_knitr来更改root.dir,是否会出现缺陷?
---
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-"))
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
```