【问题标题】:How to create R markdown with empty code chunk?如何使用空代码块创建 R 降价?
【发布时间】:2021-06-07 15:24:35
【问题描述】:

如何在 R markdown 中添加空代码块?我找到了几个选项来操作 html 以提供更多空白。但我想在众所周知的格雷码框中显示一些空行,以指示分配空间。

---
title: "Untitled"
author: "Author"
output: html_document
---

## R Markdown

```{r cars}
summary(cars)
```

## Homework 

Please calculate the mean of the `speed` variable in `cars`.

```{r}



```

【问题讨论】:

  • # Insert code here 这样的东西会做吗?
  • 如果我在 R 块中添加一行注释,它仍然不会显示任何空行。我可以用 # 开始每一行,但这在我看来是一种奇怪的解决方法。
  • 我认为某种解决方法是必要的,因为从根本上来说,knitr 将代码块视为代码,而不是您要求的预格式化文本。我添加了一些选项的答案。

标签: r r-markdown markdown


【解决方案1】:

一个 hacky 方式......几乎就在那里:

```{r, code="'\n\n\n\n'", results=F}
```

使用results = 'asis' 并依赖chunck HTML 类的可能解决方案:

```{r,  results='asis', echo=F}
cat(
  '<pre class="r">
  <code class = "hlsj"> <span class="hljs-string"> <br> <br> </span> </code>
  </pre>
  ')
```

只需添加&lt;br&gt; 即可增加行数。

【讨论】:

    【解决方案2】:

    似乎没有办法让 knitr 将完全空的块识别为块。无论块选项如何,它都会忽略它。

    您必须插入一些内容才能使其呈现,例如评论。所以你可以在两个 cmets 之间放置空行:

    ---
    output: html_document
    ---
    
    ## Homework 
    
    Please calculate the mean of the `speed` variable in `cars`.
    
    ```{r}
    # Insert code here
    
    
    
    # End
    ```
    

    或者使用 strip.white=FALSE 块选项,我们可以使用单个注释行,但奇怪的是,这仅适用于前导空格,而不是尾随空格:

    ---
    output: html_document
    ---
    
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(strip.white = FALSE)
    ```
    
    ## Homework 
    
    Please calculate the mean of the `speed` variable in `cars`.
    
    ```{r}
    
    
    
    # Insert code above
    ```
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-04
      • 2020-05-18
      • 1970-01-01
      相关资源
      最近更新 更多