我在 rmarkdown 文档中使用 units 包来输出 pdf。 但是,这些单元既不能作为内联代码也不能作为代码块。是否可以使用带 rmarkdown 的单位?
RStudio 中 rmarkdown 文档的 MWE:
---
title: "Units in R Markdown"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(units)
```
```{r define units, include=FALSE}
len <- set_units(5, mm)
wid <- set_units(10, mm)
```
In-line code: The area of the rectangle is `r len * wid`.
```{r echo = FALSE}
paste("The area of the rectangle is ", len * wid)
```
I'm expecting to see: The area of the rectangle is `r len * wid`mm^2
最佳答案
print(len * wid)
在常规 R session 中将产生相同的结果。 units 是特殊对象,需要特殊方法才能转换为字符串。
试试这个:
---
title: "Units in R Markdown"
date: "May 12, 2018"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(units)
```
```{r define units, include=FALSE}
len <- set_units(5, mm)
wid <- set_units(10, mm)
paste("The area of the rectangle is ", format(len * wid))
```
In-line code: The area of the rectangle is `r format(len * wid)`.
```{r echo = FALSE}
paste("The area of the rectangle is ", format(len * wid))
```
I'm expecting to see: The area of the rectangle is `r format(len * wid)`
关于r - 如何使用带 rmarkdown 的单位包获取 pdf 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50299748/