【问题标题】:How to use units package with rmarkdown for pdf document如何将单位包与 rmarkdown 一起用于 pdf 文档
【发布时间】:2018-10-22 07:25:23
【问题描述】:

我正在使用 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 文档的图片:

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2017-02-13
      • 1970-01-01
      • 2017-08-11
      • 2021-11-20
      • 2021-02-11
      • 2012-01-05
      • 1970-01-01
      • 2015-07-03
      • 2021-02-07
      相关资源
      最近更新 更多