yihui / knitr

A general-purpose tool for dynamic report generation in R
https://yihui.org/knitr/
2.36k stars 873 forks source link

Inline R code chunks are executed in commented code #2301

Closed thothal closed 8 months ago

thothal commented 8 months ago

Consider this minimal example:

---
title: "Inline R code gets executed in commented code"
output: html_document
---

<!-- # Section 1 -->

<!-- ```{r} -->
<!-- x <- 1 -->
<!-- ``` -->

<!-- The value of `x` is `r x`. -->

This will throw an error:

processing file: bug.Rmd |......................................................................| 100% Quitting from lines 2-15 (bug.Rmd) Error: ! object 'x' not found

Execution halted

Is this behaviour by design? My intuition was that code which is commented is not evaluated. Is it at least worth to mention this explicitly (if it is already excuse my ignorance ;))


xfun::session_info('rmarkdown')
# R version 4.2.2 (2022-10-31 ucrt)
# Platform: x86_64-w64-mingw32/x64 (64-bit)
# Running under: Windows Server x64 (build 17763), RStudio 2022.7.2.576

# Locale:
#   LC_COLLATE=English_United Kingdom.1252  LC_CTYPE=English_United Kingdom.1252   
#   LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C                           
#   LC_TIME=English_United Kingdom.1252    

# Package version:
#   base64enc_0.1.3   bslib_0.5.1       cachem_1.0.8      cli_3.6.1         digest_0.6.33     ellipsis_0.3.2   
#   evaluate_0.21     fastmap_1.1.1     fontawesome_0.5.2 fs_1.6.3          glue_1.6.2        graphics_4.2.2   
#   grDevices_4.2.2   highr_0.10        htmltools_0.5.6   jquerylib_0.1.4   jsonlite_1.8.7    knitr_1.43       
#   lifecycle_1.0.3   magrittr_2.0.3    memoise_2.0.1     methods_4.2.2     mime_0.12         R6_2.5.1         
#   rappdirs_0.3.3    rlang_1.1.1       rmarkdown_2.24    sass_0.4.7        stats_4.2.2       stringi_1.7.12   
#   stringr_1.5.0     tinytex_0.46      tools_4.2.2       utils_4.2.2       vctrs_0.6.3       xfun_0.40        
#   yaml_2.3.7       

# Pandoc version: 2.19.2

By filing an issue to this repo, I promise that

I understand that my issue may be closed if I don't fulfill my promises.

yihui commented 8 months ago

Duplicate of

Possible solutions (workarounds): https://github.com/rstudio/rmarkdown/issues/974#issuecomment-934080652 and https://github.com/yihui/knitr/issues/1567#issuecomment-401950933

github-actions[bot] commented 2 months ago

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.

yihui commented 2 weeks ago

Just for the record, commented-out R code is ignored in litedown: https://github.com/yihui/litedown