如何在rmarkdown中使用units包生成pdf文档

3

我正在使用units包在rmarkdown文档中生成pdf输出。然而,这些单位无法作为行内代码或代码块使用。是否可能在rmarkdown中使用units?

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

rmarkdown pdf文档的图片:rmarkdown pdf文档的图片:

1个回答

2
在常规的R会话中,print(len * wid)将产生相同的结果。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)`

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接