r - 如何使用带 rmarkdown 的单位包获取 pdf 文档

标签 r pdf r-markdown units-of-measurement

我在 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

rmarkdown pdf文档图片: Image of rmarkdown pdf document:

最佳答案

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

enter image description here

关于r - 如何使用带 rmarkdown 的单位包获取 pdf 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50299748/

相关文章:

r - 如何制作多种格式的 R 包插图?

r - 在 rmarkdown pdf 输出中包装比例表的列名称的有效方法

css - 在 rmd/rmarkdown 中格式化 h1、h2

r - 如何从 strptime() 中仅提取时间?

pdf - JSPDF .html() 函数返回空白 pdf 页面

ruby - 如何使用 ruby​​ 将多个图像组合成一个 PDF?

c# - 解析pdf文件

r - 堆叠条形图中每个条形的不同颜色 - 基础图形

r - ggplot 中的 Geom_area 顺序

r - 如何从 data.frame 中获取每一列作为 data.frame (而不是向量)?