R knitr 的构建代码附录不起作用

R knitr's Build Code Appendix not working

我正在尝试使用以下易慧站点的说明构建代码附录:

https://bookdown.org/yihui/rmarkdown-cookbook/code-appendix.html

问题是我没有得到任何结果,当我在 RStudio 中预览代码时没有任何反应。我注意到的一件事是代码 knitr::all_labels() returns NULL 即使我已经在我的 Rnotebook 中命名了所有 30 多个块。另外,我能够用最后一行 purl 构建一个外部文件。我尝试用基本的入门 Rnotebook 构建一个 reprex,但 reprex 没有在 RStudio 中构建,所以我将粘贴尝试过的内容。

谢谢!

代码...抱歉,我不得不删除反引号以便将所有代码块粘贴在一起。

---
title: "Reprex for Code Appendix"
author: 'ME'
date: "`r Sys.Date()`"
output: 
  html_notebook:
    code_folding: hide
---

{r setup, echo = TRUE}
knitr::opts_chunk$set(
  message = FALSE, 
  warning = FALSE, 
  # cache = TRUE,
  dev.args = list(pointsize = 11)
)
options(digits = 3, width = 80)



This is an [R Markdown](http://rmarkdown.rstudio.com) Notebook. When you execute code within the notebook, the results appear beneath the code. 

Try executing this chunk by clicking the *Run* button within the chunk or by placing your cursor inside it and pressing *Ctrl+Shift+Enter*. 

{r test}
plot(cars)


Add a new chunk by clicking the *Insert Chunk* button on the toolbar or by pressing *Ctrl+Alt+I*.

When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the *Preview* button or press *Ctrl+Shift+K* to preview the HTML file).

The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike *Knit*, *Preview* does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.

# Appendix: All code for this report

{r Code, ref.label=knitr::all_labels(), echo=TRUE}


{r get-labels, echo = FALSE}
labs = knitr::all_labels()
labs = setdiff(labs, c("setup", "get-labels"))


{r all-code, ref.label=labs, eval=FALSE}


{r, ref.label=knitr::all_labels(),echo=TRUE,eval=FALSE}


THIS DID WORK - BUILT A NEW FILE WITH THE CODE!!!!
{r code=readLines(knitr::purl('./Reprex_for_Code_Appendix.Rmd', documentation = 1)), eval = FALSE}

输出:

Reprex for Code Appendix
ME
2020-03-16
knitr::opts_chunk$set(
  message = FALSE, 
  warning = FALSE, 
  # cache = TRUE,
  dev.args = list(pointsize = 11)
)
options(digits = 3, width = 80)
This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.

Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Ctrl+Shift+Enter.

plot(cars)
Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Ctrl+Alt+I.

When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the Preview button or press Ctrl+Shift+K to preview the HTML file).

The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike Knit, Preview does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.

Appendix: All code for this report
THIS DID WORK - BUILT A NEW FILE WITH THE CODE!!!!

sessioninfo::session_info()

- Session info -----------------------------------------------------------------
 setting  value                       
 version  R version 3.6.3 (2020-02-29)
 os       Windows 10 x64              
 system   x86_64, mingw32             
 ui       RStudio                     
 language (EN)                        
 collate  English_United States.1252  
 ctype    English_United States.1252  
 tz       America/Chicago             
 date     2020-03-16                  

- Packages ---------------------------------------------------------------------
 package         * version    date       lib source        
 anomalize         0.2.0      2019-09-21 [1] CRAN (R 3.6.3)
 anytime           0.3.7      2020-01-20 [1] CRAN (R 3.6.3)
 assertthat        0.2.1      2019-03-21 [1] CRAN (R 3.6.3)
 backports         1.1.5      2019-10-02 [1] CRAN (R 3.6.1)
 base64enc         0.1-3      2015-07-28 [1] CRAN (R 3.6.0)
 bit               1.1-15.2   2020-02-10 [1] CRAN (R 3.6.2)
 bit64             0.9-7      2017-05-08 [1] CRAN (R 3.6.2)
 blob              1.2.1      2020-01-20 [1] CRAN (R 3.6.3)
 broom             0.5.5      2020-02-29 [1] CRAN (R 3.6.3)        
 callr             3.4.2      2020-02-12 [1] CRAN (R 3.6.3)
 cellranger        1.1.0      2016-07-27 [1] CRAN (R 3.6.3)
 cli               2.0.2      2020-02-28 [1] CRAN (R 3.6.3)
 colorspace        1.4-1      2019-03-18 [1] CRAN (R 3.6.1)
 crayon            1.3.4      2017-09-16 [1] CRAN (R 3.6.3)
 data.table        1.12.8     2019-12-09 [1] CRAN (R 3.6.3)
 DBI               1.1.0      2019-12-15 [1] CRAN (R 3.6.3)
 dbplyr            1.4.2      2019-06-17 [1] CRAN (R 3.6.3)
 desc              1.2.0      2018-05-01 [1] CRAN (R 3.6.3)
 devtools          2.2.2      2020-02-17 [1] CRAN (R 3.6.3)
 digest            0.6.25     2020-02-23 [1] CRAN (R 3.6.3)
 dplyr           * 0.8.4      2020-01-31 [1] CRAN (R 3.6.3)
 ellipsis          0.3.0      2019-09-20 [1] CRAN (R 3.6.3)
 evaluate          0.14       2019-05-28 [1] CRAN (R 3.6.3)
 fable           * 0.1.2      2020-01-29 [1] CRAN (R 3.6.3)
 fabletools      * 0.1.2      2020-01-29 [1] CRAN (R 3.6.3)
 fansi             0.4.1      2020-01-08 [1] CRAN (R 3.6.3)
 farver            2.0.3      2020-01-16 [1] CRAN (R 3.6.3)
 fastmap           1.0.1      2019-10-08 [1] CRAN (R 3.6.3)
 feasts          * 0.1.2      2020-01-17 [1] CRAN (R 3.6.3)
 forcats         * 0.5.0      2020-03-01 [1] CRAN (R 3.6.3)
 fs                1.3.1      2019-05-06 [1] CRAN (R 3.6.3)
 generics          0.0.2      2018-11-29 [1] CRAN (R 3.6.3)
 ggplot2         * 3.3.0      2020-03-05 [1] CRAN (R 3.6.3)
 glue              1.3.1      2019-03-12 [1] CRAN (R 3.6.3)
 gtable            0.3.0      2019-03-25 [1] CRAN (R 3.6.3)
 haven             2.2.0      2019-11-08 [1] CRAN (R 3.6.3)
 highr             0.8        2019-03-20 [1] CRAN (R 3.6.3)
 hms               0.5.3      2020-01-08 [1] CRAN (R 3.6.3)
 htmltools         0.4.0      2019-10-04 [1] CRAN (R 3.6.3)
 httpuv            1.5.2      2019-09-11 [1] CRAN (R 3.6.3)
 httr              1.4.1      2019-08-05 [1] CRAN (R 3.6.3)
 jsonlite          1.6.1      2020-02-02 [1] CRAN (R 3.6.3)
 kableExtra        1.1.0      2019-03-16 [1] CRAN (R 3.6.3)
 knitr           * 1.28       2020-02-06 [1] CRAN (R 3.6.3)
 labeling          0.3        2014-08-23 [1] CRAN (R 3.6.0)
 later             1.0.0      2019-10-04 [1] CRAN (R 3.6.3)
 lattice           0.20-38    2018-11-04 [2] CRAN (R 3.6.3)
 lifecycle         0.2.0      2020-03-06 [1] CRAN (R 3.6.3)      
 lubridate         1.7.4      2018-04-11 [1] CRAN (R 3.6.3)
 magrittr          1.5        2014-11-22 [1] CRAN (R 3.6.3)
 memoise           1.1.0      2017-04-21 [1] CRAN (R 3.6.3)
 mime              0.9        2020-02-04 [1] CRAN (R 3.6.2)
 miniUI            0.1.1.1    2018-05-18 [1] CRAN (R 3.6.3)
 modelr            0.1.6      2020-02-22 [1] CRAN (R 3.6.3)
 multidplyr        0.0.0.9000 2020-02-29 [1] local         
 munsell           0.5.0      2018-06-12 [1] CRAN (R 3.6.3)
 nlme              3.1-144    2020-02-06 [2] CRAN (R 3.6.3)
 pillar            1.4.3      2019-12-20 [1] CRAN (R 3.6.3)
 pkgbuild          1.0.6      2019-10-09 [1] CRAN (R 3.6.3)
 pkgconfig         2.0.3      2019-09-22 [1] CRAN (R 3.6.3)
 pkgload           1.0.2      2018-10-29 [1] CRAN (R 3.6.3)
 prettyunits       1.1.1      2020-01-24 [1] CRAN (R 3.6.3)
 processx          3.4.2      2020-02-09 [1] CRAN (R 3.6.3)
 promises          1.1.0      2019-10-04 [1] CRAN (R 3.6.3)
 ps                1.3.2      2020-02-13 [1] CRAN (R 3.6.3)
 purrr           * 0.3.3      2019-10-18 [1] CRAN (R 3.6.3)
 R6                2.4.1      2019-11-12 [1] CRAN (R 3.6.3)
 Rcpp              1.0.3      2019-11-08 [1] CRAN (R 3.6.3)
 readr           * 1.3.1      2018-12-21 [1] CRAN (R 3.6.3)
 readxl            1.3.1      2019-03-13 [1] CRAN (R 3.6.3)
 remotes           2.1.1      2020-02-15 [1] CRAN (R 3.6.3)
 reprex            0.3.0      2019-05-16 [1] CRAN (R 3.6.3)
 rlang             0.4.5      2020-03-01 [1] CRAN (R 3.6.3)
 rmarkdown         2.1        2020-01-20 [1] CRAN (R 3.6.3)
 RODBC             1.3-16     2019-09-03 [1] CRAN (R 3.6.1)
 RPostgres       * 1.2.0      2019-12-18 [1] CRAN (R 3.6.3)
 rprojroot         1.3-2      2018-01-03 [1] CRAN (R 3.6.3)
 rstudioapi        0.11       2020-02-07 [1] CRAN (R 3.6.3)
 rvest             0.3.5      2019-11-08 [1] CRAN (R 3.6.3)
 scales            1.1.0      2019-11-18 [1] CRAN (R 3.6.3)
 sessioninfo       1.1.1      2018-11-05 [1] CRAN (R 3.6.3)
 shiny           * 1.4.0      2019-10-10 [1] CRAN (R 3.6.3)
 sodium            1.1        2017-03-30 [1] CRAN (R 3.6.3)
 stringi           1.4.6      2020-02-17 [1] CRAN (R 3.6.2)
 stringr         * 1.4.0      2019-02-10 [1] CRAN (R 3.6.3)
 testthat          2.3.2      2020-03-02 [1] CRAN (R 3.6.3)
 tibble          * 2.1.3      2019-06-06 [1] CRAN (R 3.6.2)
 tibbletime        0.1.3      2019-09-20 [1] CRAN (R 3.6.3)
 tidyr           * 1.0.2      2020-01-24 [1] CRAN (R 3.6.3)
 tidyselect        1.0.0      2020-01-27 [1] CRAN (R 3.6.3)
 tidyverse       * 1.3.0      2019-11-21 [1] CRAN (R 3.6.3)
 tsibble         * 0.8.6      2020-01-31 [1] CRAN (R 3.6.3)
 urca              1.3-0      2016-09-06 [1] CRAN (R 3.6.3)
 usethis           1.5.1      2019-07-04 [1] CRAN (R 3.6.3)
 utf8              1.1.4      2018-05-24 [1] CRAN (R 3.6.3)
 vctrs             0.2.3      2020-02-20 [1] CRAN (R 3.6.3)
 viridisLite       0.3.0      2018-02-01 [1] CRAN (R 3.6.3)
 webshot           0.5.2      2019-11-22 [1] CRAN (R 3.6.3)
 whisker           0.4        2019-08-28 [1] CRAN (R 3.6.3)
 withr             2.1.2      2018-03-15 [1] CRAN (R 3.6.3)
 xfun              0.12       2020-01-13 [1] CRAN (R 3.6.3)
 xml2              1.2.2      2019-08-09 [1] CRAN (R 3.6.3)
 xtable            1.8-4      2019-04-21 [1] CRAN (R 3.6.3)
 yaml              2.2.1      2020-02-01 [1] CRAN (R 3.6.2)

[1] C:/Apps/R/R_LIBS_USER
[2] C:/Apps/R/R-3.6.3/library

knitr::all_labels() returns NULL 如果在全编织过程中没有调用。当您 运行 R Markdown Notebook 中的单个代码块时,文档未完全编织。

要制作 knitr::all_labels() return 所有块标签,你不应该使用笔记本模式或预览笔记本,而是编织文档(例如,到 html_document 输出格式).