繁体   English   中英

如何使用单位包与rmarkdown的pdf文件

[英]How to use units package with rmarkdown for pdf document

我在rmarkdown文档中使用单位包进行pdf输出。 但是,这些单元既不能用作内联代码,也不能用作代码块。 是否可以使用rmarkdown的单位?

RSt中的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文件的图片:

普通R会话中的print(len * wid)将产生相同的结果。 单位是特殊对象,需要特殊方法才能转换为字符串。 尝试这个:

---
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)`

在此输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM