如何将带有 rmarkdown 的单位包用于 pdf 文档 [英] How to use units package with rmarkdown for pdf document

查看:71
本文介绍了如何将带有 rmarkdown 的单位包用于 pdf 文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using the units package in an rmarkdown document for pdf output. However, the units do not function either in-line code or as code chunks. Is it possible to use units with rmarkdown?

RStudio 中 rmarkdown 文档的 MWE:

MWE for rmarkdown document in RStudio:

---
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 会话中将产生相同的结果.units 是特殊的对象,需要特殊的方法才能转换为字符串.试试这个:

print(len * wid) in regular R session will produce the same result. units are special objects and need special methods to be converted to a string. Try this:

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

这篇关于如何将带有 rmarkdown 的单位包用于 pdf 文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆