小编Yih*_*Xie的帖子

Knitr:从代码块打印文本为R markdown

我有以下R Markdown文档:

---
title: "Test"
output: html_document
---

```{r cars, echo=FALSE}
myCondition <- TRUE
if(myCondition) {
  print("## Car Summary")
}
summary(cars)
```
Run Code Online (Sandbox Code Playgroud)

当我将其编织为HTML时,"Car Summary"标题以"类似终端"的等宽字体呈现,如下所示:

## [1] "## Car Summary"
Run Code Online (Sandbox Code Playgroud)

但我希望它呈现为标题.我该如何实现这一目标?

r knitr r-markdown

6
推荐指数
1
解决办法
8237
查看次数

如何在关闭R之前从管道连接获取输出?

在R中,我们可以使用pipe()并写入它来打开管道连接.我观察到以下我不太了解的情况.我们以python管道为例:

z = pipe('python', open='w+')

cat('x=1\n', file=z)
cat('print(x)\n', file=z)
cat('print(x+2)\n', file=z)
cat('print(x+2\n', file=z)
cat(')\n', file=z)

close(z)
Run Code Online (Sandbox Code Playgroud)

我期待的是输出print()会立即显示在R控制台中,但事实是输出仅在我关闭管道连接后才出现:

> z = pipe('python', open='w+')
> 
> cat('x=1\n', file=z)
> cat('print(x)\n', file=z)
> cat('print(x+2)\n', file=z)
> cat('print(x+2\n', file=z)
> cat(')\n', file=z)
> 
> close(z)
1
3
3
Run Code Online (Sandbox Code Playgroud)

所以我的问题是,在关闭连接之前如何获得输出?请注意,似乎无法使用以下capture.output()任一方式捕获输出:

> z = pipe('python', open='w+')
> 
> cat('x=1\n', file=z)
> cat('print(x)\n', file=z)
> cat('print(x+2)\n', file=z)
> cat('print(x+2\n', file=z)
> cat(')\n', file=z)
> 
> x = capture.output(close(z))
1 …
Run Code Online (Sandbox Code Playgroud)

r stdout pipe knitr

5
推荐指数
1
解决办法
866
查看次数

如何编织/编织到不同的文件名?

默认情况下,knit/Sweave 命令将生成一个与 .Rnw 文件同名的 tex 文件。我想给它和后续的 pdf 文件起一个不同的名称,可以吗?

r sweave knitr

5
推荐指数
1
解决办法
2381
查看次数

`knitr` 可以抑制 sql 块中的执行或输出吗?

下面的文档运行 sql 并显示结果。我不希望通过不运行块或隐藏输出来显示任何输出。

有没有办法做到这一点?

---
output: html_document
---

## Hide SQL Output

First, set up a temporary database:

```{r}
library(RSQLite)
conn <- dbConnect(SQLite(), tempfile())
df <- data.frame(value = runif(1))
dbWriteTable(conn, "TestTable", df)
```

Now show a query, but try not to run it, and try
to hide the output.  Neither works:  it runs, and 
displays the table.

```{sql connection = conn,results="hide",eval=FALSE}
SELECT * FROM TestTable;
```
Run Code Online (Sandbox Code Playgroud)

我得到这个输出: 在此处输入图片说明

r knitr

5
推荐指数
1
解决办法
936
查看次数

设置 R bookdown 输入目录

我想使用 bookdown 将一系列.Rmd文件解析为一本书。如果我.Rmd的目录与主index.Rmd文件位于同一目录中,那么一切正常且花花公子。但是,这些.Rmd文件是从另一个来源自动生成的,我想将输入文件保存在一个子目录中。有可能这样做吗?

我可以在其中包含文件名,_bookdown.yml但我不希望每次.Rmd添加新文件时都更新该文件。

当前版本_bookdown.yml看起来像这样

new_session: yes
rmd_files:
  html: ["input/index.Rmd", "input/01-file.Rmd"]
  latex: ["input/index.Rmd", "input/01-file.Rmd"]
Run Code Online (Sandbox Code Playgroud)

我可以在脚本中移动文件,但希望它可以通过bookdown它自己实现。

r bookdown

5
推荐指数
1
解决办法
925
查看次数

在 Bookdown 中渲染附录图编号

Bookdown 是一个很棒的软件包,我很期待看到它是如何发展的,但是现在我pdf_document2在以附录形式呈现图形编号时遇到了麻烦。特别是当带有标题的图形在附录中时,图形编号应采用 A.1、A.2、B.1、B.2 等形式,但图形编号被视为带有编号 3.1 的正常部分, 3.2、4.1、4.2等,其中附录分别为第三和第四节。下面是一个例子:

---
title: "Untitled"
author: "John Doe"
date: "November 18, 2016"
documentclass: article
output:
  bookdown::pdf_document2:
    toc: yes
    fig_caption: yes
    number_sections: yes
linkcolor: blue
---

# Chapter One

```{r a, fig.cap="rabble rabble"}
plot(cars) # a scatterplot
```

```{r b, fig.cap="rabble rabble"}
plot(cars) # a scatterplot
```

# Chapter Two

# (APPENDIX) Appendix {-}
# Appendix A

```{r c, fig.cap="rabble rabble"}
plot(cars) # a scatterplot
```

# Appendix B

```{r d, fig.cap="rabble rabble"}
plot(cars) …
Run Code Online (Sandbox Code Playgroud)

r bookdown

5
推荐指数
1
解决办法
1330
查看次数

在RStudio升级1.0.44后,opts_knit $ set(root.dir = path)不起作用

我升级到RStudio 1.0.44,似乎knitr :: opts_knit $ set(root.dir = path)其中path是我的目录不像以前那样工作.它抛出一条消息:

The working directory was changed to /... inside a notebook chunk. The working
directory will be reset when the chunk is finished running. Use the knitr
root.dir option in the setup chunk to change the the working directory for
notebook chunks.
Run Code Online (Sandbox Code Playgroud)

此消息现在将出现在以下每个命令中.请注意,我还没有编织rmd.我只是在运行命令.通过setwd()直接在命令行中设置工作目录会返回getwd()中的正确路径,但是再次加载具有相对路径(./ ...)的文件将返回上面的消息.RStudio 0.99.896完全相同的rmd工作正常.我错过了什么?

sessionInfo()

R version 3.3.0 (2016-05-03)  
Platform: x86_64-w64-mingw32/x64 (64-bit)  
>Running under: Windows 7 x64 (build 7601) Service Pack 1  

other attached packages:
[1] scales_0.4.0     ggplot2_2.1.0    xtable_1.8-2     data.table_1.9.6  
[5] dplyr_0.4.3 …
Run Code Online (Sandbox Code Playgroud)

r working-directory rstudio knitr

5
推荐指数
1
解决办法
4067
查看次数

更灵活的引文格式

我喜欢在我的 r bookdown 项目中使用引文。

格式不是很灵活,我可以使用@citeID 获取“作者(年份)”或[@citeID] 获取“(作者年份)”。

有时我只需要“(Year)”或“Year”,或者我可能只需要“Author Year”,根本不需要括号。

最后,我需要添加对引文的页面引用,例如“作者(年份:页面)”

任何人都可以给我一个提示,相关代码在源代码中的位置,以便我可以开始添加一些上述引用格式?

markdown r pandoc r-markdown bookdown

5
推荐指数
1
解决办法
262
查看次数

更改表格标题的位置 (tufte_handout)

这是一个 R -> Latex 问题。\n我使用 tufte_handout

\n\n

我需要将全角表格的标题放在表格下方的页边距中,因为表格太宽了。

\n\n

表格/标题冲突示例

\n\n

这是 Rmarkdown 文件

\n\n
---\ntitle: "Untitled"\nauthor: "A. Nonymous"\ndate: "19/8/2017"\nheader-includes:\n   - \\usepackage{array}\noutput:\n  tufte::tufte_handout:\n    citation_package: natbib\n    keep_tex: yes\n    latex_engine: xelatex\n---\n\n```{r setup, include=FALSE}\nlibrary(tidyverse)\nlibrary(tibble)\nlibrary(knitr)\nlibrary(kableExtra)\nlibrary(tufte)\n\nknitr::opts_chunk$set(echo = TRUE)\n\noptions(knitr.table.format = "latex") \n```\n\n\n# table\n\n\n```{r table-1, fig.fullwidth=T, echo=FALSE}\n\ntmp <- tibble(`long titre de texte de colonne` = 1:3, \n              `encore un long titre de colonne` = 101:103,\n       `c\'est tr\xc3\xa8s long l\xc3\xa0 aussi (trop)`=31:33, \n       `mais pourquoi est-ce si long ? (oui, pourquoi?)`=c("a","b","b"))\ntmp <- as.data.frame(tmp)\n\nknitr::kable(tmp,booktabs=TRUE,caption="test de test de table, mais c\'est …
Run Code Online (Sandbox Code Playgroud)

r knitr r-markdown tufte bookdown

5
推荐指数
1
解决办法
1856
查看次数

在书本图形环境中包含TikZ代码

我想在bookdown文档中添加TikZ图形,以便包含一些精美的图形。

我的主要输出格式是LaTeX,这意味着我基本上可以在Rmarkdown文件中逐字包含TikZ图形,并且可以很好地呈现。但是,有两个问题困扰着我:

  • 我希望TikZ图形成为图形环境的一部分(用于编号,标题等)。
  • 我希望能够将相同的代码呈现给PDF(LaTeX)和Gitbook(HTML)。

现在,我有以下块,当我将其渲染为pdf时,可以很好地生成相关图形。

```{r, echo=FALSE, engine='tikz', out.width='90%', fig.ext='pdf', fig.cap='Some caption.'}
\begin{tikzpicture}[scale=.7]
\draw [fill=gray!30,very thick] (0,-1) rectangle (5,1);
\draw [very thick] (5, 0) -- (13,0);
\node [below] at (2,-1) {\large Hello};
\node [below, align=center] at (0,-1) {\large Two\\ lines};
\end{tikzpicture}
```
Run Code Online (Sandbox Code Playgroud)

但是,代码有两个问题:

  1. 渲染到gitbook时,我没有得到任何输出(使用knitrbookdown)。但是,我确实获得了图形标题,如果我渲染到html_document它,那么它也可以工作,并且我可以看到图形。
  2. 对于PDF,使用计算机现代字体呈现文本。我真的很想更改此设置,并且LaTeX文档中的主要字体已经设置为其他字体。但是,由于代码是由TikZ引擎本地渲染然后插入的,因此它不是完整LaTeX文档的一部分。我可以呈现代码之前添加一些TikZ引擎包含的LaTeX选项,软件包等吗?

如果还有其他方法可以将TikZ代码作为图形环境的一部分包含在内,那么我很高兴知道。

更新:我猜第二点可以通过设置文件中engine.opts = list(template = "latex/tikz2pdf.tex")包含LaTeX的必要设置的位置来解决tikz2pdf.tex。该文件是使用LaTeX读取的,但xelatex由于要使用fontspecLaTex软件包,因此我想使用该文件进行解析。仍然可以更改吗?

r knitr r-markdown bookdown tikzdevice

5
推荐指数
1
解决办法
980
查看次数