- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
语言引擎
是一个很棒的 knitr
功能。我们可以在 knitr 中使用任何语言,包括但不限于 R。但是如果我想使用一个 chunck 中定义的结果/变量,在另一个 chunck 中使用相同的语言或另一种语言怎么办(更性感的选择)?
最好用一些代码来解释我的想法。这里我定义了 4 个 block ,2 个在 python 中,2 个在 R 中。
首先我在python中定义了一个变量x
:
## I define engien path explicitly here (TODO: set it globally)
```{r,engine='python',engine.path='C:/Anaconda/python.exe' }
x = 1
print x
```
## 1
现在尝试在新的 python chunck 中使用 x:
```{r,engine='python',engine.path='C:/Anaconda/python.exe' }
x = x +1
print x
```
没有错误,但结果惊人,看起来 x 在这里是 NULL。现在,如果我尝试在新的 R chunck 中使用 x:
```{r fig.width=7, fig.height=6}
x +1
y = 2
```
## Error: object 'x' not found
我得到一个错误。现在,如果我尝试在新的 R block 中使用 y,它工作正常。 r 引擎可以使用之前 R chunck 中定义的变量。请注意,这不适用于 python。
```{r fig.width=7, fig.height=6}
y+3
```
## [1] 5
为什么 R 和 python 之间的行为在这里存在差异?它是由于 R 作用域规则而结构化的,还是只是尚未在 knitr 中实现的 future ?或者可能是一个错误?
最佳答案
这是记录在案的行为。
参见 http://yihui.name/knitr/demo/engines/
Except engine='R' (default), all chunks are executed in separate sessions, so the variables cannot be directly shared. If we want to make use of objects created in previous chunks, we usually have to write them to files (as side effects). For the bash engine, we can use Sys.setenv() to export variables from R to bash (example)
关于python - 在 knitr 中以不同语言定义的 block 之间混合变量/结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17982967/
我正在用 Bookdown 写一份报告,我有兴趣水平地复制一系列图片,并用一个描述所有图片的标题。如果我不定义 fig.cap,我会得到正确的图片,如下所示: ```{r, out.width = "
我用过dplyr对于某些分析和特定代码,完成操作大约需要 30 秒。在生成的 HTML 中,我得到了类似这样的很长的输出(再现最后几行): |=============================
我的 last two问题只是令人尴尬,但我相信这是真正的交易。我的系统是新安装的,所以除了操作系统之外的版本都是最新的:Windows 7 SP1、R-3.0.1、Lyx 2.0.6、knitr 1
我找不到将语法上可接受的 RStudio 样式折叠插入外部 R 代码文件的方法,该文件设置为从 knitr 文档中使用。或者我错过了什么。有几种方法可以做到这一点:1) 允许代码头,例如: ## @k
我有一个包含以下代码的小书本“书”: A concept map is a diagram that shows 'ideas' and the relationship between those
像这样的东西,但它不起作用: ```{r examples, engine="bash"} export EXAMPLES="example/path" ``` ```{r example1,
我是 latex 和 knitr 的新手,当我使用 echo=FALSE 时,R block 的输出有问题。下面的 .Rnw 代码按预期工作,即输出有 1. some code 2.
有没有办法使用 .Rmd 文件 直接在 Jupyter 中?换句话说,有没有办法让 jupyter 渲染这样的文件:https://github.com/yihui/knitr-examples/bl
是否可以在 markdown 文件中使用 knitr 的 pandoc() 函数和嵌入的 pandoc 配置来更改默认的 pandoc latex 模板选项? 例如,如果我在 foo.md 的开头使用
我不熟悉使用 rmarkdown 和 knitr 来制作 .docx word 文档。 rmarkdown 引用指南指出,使用 -- 给出一个破折号,而 --- 给出一个破折号。 如果我将我的 .Rm
我想知道是否有一种自动化的方法可以使评论不打印在输出中。现在,我正在使用以下内容来省略评论: >= ## load relimp package library(relimp) ## calculat
我已经根据文档在 Knitr 上添加了自定义语言引擎。 require(knitr) knit_engines$set(upper = function(options) { code <- pa
我正在使用 Rmarkdown 生成一个 PDF 文档,我想在其中手动定义图形编号。 下面是一个 block 的例子: ```{r chunk26, fig.cap = "Fig. 5.3 My fi
这可能是一个极端情况,但我正在尝试使用 knitr 创建一个投影仪演示文稿,我想在其中使用不同的公式作为函数的参数来显示代码块。我发现当使用覆盖时,代码块中的波浪号消失了。有没有办法让它们显示? 这是
我尝试编译 YiHui 的 BIG5 example (有中文内容的knitr)。我使用了 Mac OSX snowleopard、最新的 RStudio、pdfLatex(结果与 XeLatex 相
这听起来像是一个愚蠢的问题,但是当我使用 RStudio 服务器从 Rmd 文件编译 pdf 文档时,我想知道 .tex 文件保存在哪里。 我添加了 keep_tex 选项,因此 Rmd 的标题如下所
在 knitr可以在代码块中使用其他语言。例如,我们可以使用: ```{python} Some python code ``` 我们可以使用 R引擎内联`r一些R代码` 中是否可以使用其他语言?内联
例如,在编写演讲幻灯片时,我们经常会遇到这样一种情况,我们希望内联代码输出为 source code = result。 .所以例如 "foofoofoo qt(p = 0.95, df = 24)
有没有办法,在 knitr 中将 fig.cap 移动到图形上方?我需要上面的它们,以便当为某个表选择图列表的超链接时,它会导航到图。 Right now the caption and thus h
如果它在同一目录中,我可以对子文档使用以下代码。 >= @ 如果子文档不在主文档的同一目录中,我想知道如何使用子文档。预先感谢您的帮助和时间。 最佳答案 这可以被认为是一个错误。现在我已经把它修好了
我是一名优秀的程序员,十分优秀!