如何从代码块中的数据框中获取数字(或文本)到 RMarkdown html 输出中的文本?
How do I get a number (or text) from a data frame in a code chunk in to text in RMarkdown html output?
当输出是 word 文档时我可以让它工作,但当输出是 html.
---
title: "R Notebook"
output:
word_document: default
html_notebook: default
---
```{r, include = FALSE}
library(tidyverse)
mtcars
```
Number `r mtcars %>% select(mpg) %>% slice(1)`
在 word 中输出是
Number 21
但是当输出为html时,我得到了一个整体table。
有没有办法只获取 html 输出中的文本?
它会在文本中使用,所以我不想要 table。
slice
returns class 数据框因此你得到输出 table
mtcars %>% select(mpg) %>% slice(1) %>% class
#[1] "data.frame"
您需要输出为矢量,因此任何能够将最终输出作为矢量的方式都可以。在这里,我使用 pull
(请记住您使用的是 tidyverse
)将输出作为向量(此处为 numeric
)。
mtcars %>% select(mpg) %>% slice(1) %>% pull %>% class
#[1] "numeric"
也一样,
---
title: "R Notebook"
output:
word_document: default
html_notebook: default
---
```{r, include = FALSE}
library(tidyverse)
mtcars
```
Number `r mtcars %>% select(mpg) %>% slice(1) %>% pull`
这不会更改 Word 中的输出,它仍会像以前一样工作。
当输出是 word 文档时我可以让它工作,但当输出是 html.
---
title: "R Notebook"
output:
word_document: default
html_notebook: default
---
```{r, include = FALSE}
library(tidyverse)
mtcars
```
Number `r mtcars %>% select(mpg) %>% slice(1)`
在 word 中输出是
Number 21
但是当输出为html时,我得到了一个整体table。
有没有办法只获取 html 输出中的文本?
它会在文本中使用,所以我不想要 table。
slice
returns class 数据框因此你得到输出 table
mtcars %>% select(mpg) %>% slice(1) %>% class
#[1] "data.frame"
您需要输出为矢量,因此任何能够将最终输出作为矢量的方式都可以。在这里,我使用 pull
(请记住您使用的是 tidyverse
)将输出作为向量(此处为 numeric
)。
mtcars %>% select(mpg) %>% slice(1) %>% pull %>% class
#[1] "numeric"
也一样,
---
title: "R Notebook"
output:
word_document: default
html_notebook: default
---
```{r, include = FALSE}
library(tidyverse)
mtcars
```
Number `r mtcars %>% select(mpg) %>% slice(1) %>% pull`
这不会更改 Word 中的输出,它仍会像以前一样工作。