从 R 中的 read_line() 函数打印代码时删除括号和散列

Remove brackets and hashes when printing code from read_line() function in R

我正在嵌入 文件。

当我在没有 运行 的情况下在我的 Rmarkdown 文件中嵌入 R 中的脚本并且编织成 PDF 或 HTML 时,每行包含两个散列并括在括号中。

我想删除这些散列和外括号。

这是在我的 .Rmd 文件中实现的代码:

```{r include=FALSE}
library(readr)
script1 <- read_lines("script1.R")
```

```{r echo=FALSE}
print(script1)
```

现在让我们看看script1.R

的代码
  # 1. Cargamos los paquetes necesarios ####
  library(readODS)
  library(dplyr)
  library(purrr)
  library(tidyr)
  library(ggplot2)
  library(gridExtra)
  library(ggpubr)
  library(readr)

  # 2. Cargamos los datos desde el archivo de LibreOffice Calc ####
  path <- "Data/xy_coord_div.ods"

  # Creamos una lista con los datos de todos los tipos de dientes
  xy_coord <- path %>%
    ods_sheets() %>%
    set_names() %>%
    map(read_ods, skip = 1, path = path)

当我将 .Rmd 文件编织到 HTML 时,我看到的代码是这样的:

##  [1] "  # 1. Cargamos los paquetes necesarios ####"                                             
##  [2] "  library(readODS)"                                                                       
##  [3] "  library(dplyr)"                                                                         
##  [4] "  library(purrr)"                                                                         
##  [5] "  library(tidyr)"                                                                         
##  [6] "  library(ggplot2)"                                                                       
##  [7] "  library(gridExtra)"                                                                     
##  [8] "  library(ggpubr)"                                                                        
##  [9] "  library(readr)"                                                                         
## [10] ""                                                                                         
## [11] "  # 2. Cargamos los datos desde el archivo de LibreOffice Calc ####"                      
## [12] "  path <- \"Data/xy_coord_div.ods\""                                                      
## [13] ""                                                                                         
## [14] "  # Creamos una lista con los datos de todos los tipos de dientes"                        
## [15] "  xy_coord <- path %>%"                                                                   
## [16] "    ods_sheets() %>%"                                                                     
## [17] "    set_names() %>%"                                                                      
## [18] "    map(read_ods, skip = 1, path = path)"                                                 
## [19] ""                                       

你也可以看到在处理内括号时,多写了一些符号。

如何在转换为其他格式时设置代码样式?

Use the chunk option comment='' 去除散列,使用 cat() 去除括号。

```{r include=FALSE}
script1 <- readLines("script1.R")
```

```{r echo=FALSE, comment=''}
cat(script1, sep = '\n')
```

或与 xfun::file_string() 一步到位:

```{r echo=FALSE, comment=''}
xfun::file_string("script1.R")
```