Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

do not evaluate inline R inside HTML comment tags #1619

Closed
maxheld83 opened this issue Aug 12, 2019 · 8 comments
Closed

do not evaluate inline R inside HTML comment tags #1619

maxheld83 opened this issue Aug 12, 2019 · 8 comments
Labels
feature a feature request or enhancement theme: knitr concerns knitr package

Comments

@maxheld83
Copy link
Contributor

maxheld83 commented Aug 12, 2019

Possible duplicate of #974, but that's about chunks, not inline R, so I'm not sure.

Reproducible example

Render the below example document: rmarkdown::render("reprex.Rmd")

---
title: "Untitled"
output: html_document
---

Here is some inline R `r 1+1`.

<!-- This should not be shown, because it is inside a comment `r 2+2`. -->

<!-- This should not be evaluated, because it is inside a comment `r stop()`. -->

Expected Behavior

  1. Rmarkdown should not show the second inline R expression in the resulting HTML.
  2. Rmarkdown should also not evaluate the third inline R expression.

... because both are inside HTML comment tags.

Observed Behavior

  1. 🛑 The third inline R expression does get evaluated, and the whole rmarkdown::render("reprex.Rmd") fails.

xfun::session_info()

R version 3.6.1 (2019-07-05)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Mojave 10.14.6, RStudio 1.2.1565

Locale: en_US.UTF-8 / en_US.UTF-8 / en_US.UTF-8 / C / en_US.UTF-8 / en_US.UTF-8

Package version:
  askpass_1.1               assertthat_0.2.1          backports_1.1.4           base64enc_0.1.3          
  BH_1.69.0.1               broom_0.5.2               callr_3.3.1               cellranger_1.1.0         
  cli_1.1.0                 clipr_0.7.0               coda_0.19.3               colorspace_1.4-1         
  compiler_3.6.1            crayon_1.3.4              curl_4.0                  data.table_1.12.2        
  DBI_1.0.0                 dbplyr_1.4.2              digest_0.6.20             dplyr_0.8.3              
  dtt_0.1.2                 ellipsis_0.2.0.1          english_1.2.3             evaluate_0.14            
  extrafont_0.17            extrafontdb_1.0           fansi_0.4.0               fastmatch_1.1.0          
  forcats_0.4.0             fs_1.3.1                  generics_0.0.2            ggplot2_3.2.1            
  ggrepel_0.8.1             glue_1.3.1                graphics_3.6.1            grDevices_3.6.1          
  grid_3.6.1                gtable_0.3.0              haven_2.1.1               highr_0.8                
  hms_0.5.0                 htmltools_0.3.6           httr_1.4.1                hunspell_3.0             
  igraph_1.2.4.1            ISOcodes_2019.4.22        janeaustenr_0.1.5         jsonlite_1.6             
  knitr_1.24                koRpus_0.11-5             koRpus.lang.en_0.1-3      labeling_0.3             
  lattice_0.20-38           lazyeval_0.2.2            lexicon_1.2.1             lubridate_1.7.4          
  magrittr_1.5              markdown_1.1              MASS_7.3.51.4             Matrix_1.2-17            
  methods_3.6.1             mgcv_1.8.28               mgsub_1.7.1               mime_0.7                 
  modelr_0.1.5              modeltools_0.2-22         munsell_0.5.0             network_1.15             
  nlme_3.1-141              NLP_0.2-0                 openssl_1.4.1             parallel_3.6.1           
  pillar_1.4.2              pkgconfig_2.0.2           plogr_0.2.0               plyr_1.8.4               
  prettyunits_1.0.2         printr_0.1                processx_3.4.1            progress_1.2.2           
  proxyC_0.1.5              ps_1.3.0                  purrr_0.3.2               qdapRegex_0.7.2          
  quanteda_1.5.1            R6_2.4.0                  RColorBrewer_1.1.2        Rcpp_1.0.2               
  RcppArmadillo_0.9.600.4.0 RcppEigen_0.3.3.5.0       RcppParallel_4.4.3        readr_1.3.1              
  readxl_1.3.1              rematch_1.0.1             reprex_0.3.0              reshape2_1.4.3           
  reticulate_1.13           rlang_0.4.0               rmarkdown_1.14            RSpectra_0.15.0          
  rstudioapi_0.10           Rttf2pt1_1.3.7            rvest_0.3.4               scales_1.0.0             
  selectr_0.4.1             slam_0.1-45               sna_2.4                   SnowballC_0.6.0          
  spacyr_1.2                splines_3.6.1             statnet.common_4.3.0      stats_3.6.1              
  stats4_3.6.1              stopwords_1.0             stringi_1.4.3             stringr_1.4.0            
  sylly_0.1-5               sylly.en_0.1-3            sys_3.2                   syuzhet_1.0.4            
  textclean_0.9.3           textshape_1.6.0           textstem_0.1.4            tibble_2.1.3             
  tidyr_0.8.3               tidyselect_0.2.5          tidytext_0.2.2            tidyverse_1.2.1          
  tinytex_0.15              tm_0.7-6                  tokenizers_0.2.1          tools_3.6.1              
  topicmodels_0.2-8         utf8_1.1.4                utils_3.6.1               vctrs_0.2.0              
  viridisLite_0.3.0         whisker_0.3.2             withr_2.1.2               xfun_0.8                 
  xml2_1.2.2                yaml_2.2.0                zeallot_0.1.0             zoo_1.8.6   
@yihui
Copy link
Member

yihui commented Aug 12, 2019

Yes, that is essentially the same issue as #974. Thanks for your vote!

@sbalci
Copy link

sbalci commented Dec 8, 2020

I vote for this as well. Thank you.
I was directed here from question: https://community.rstudio.com/t/comment-inline-r-code-to-avoid-running-the-code/90050

commenting-inline-r-code

@cderv cderv added feature a feature request or enhancement and removed enhancement labels Apr 14, 2021
@cderv
Copy link
Collaborator

cderv commented Jan 11, 2022

FWIW there is now a comment engine (knitr v1.37) that allows to comment any part of the document and skip anything inside

---
title: "Untitled"
output: html_document
---

Here is some inline R `r 1+1`.

```{comment}
This should not be shown, because it is inside comment block  `r 2+2`
This should not be evaluated, because it is inside a comment block `r stop()`
```

or from the community question

---
title: "Untitled"
output: html_document
---

```{r, eval = FALSE}
myoutput <- 2 + 2 
```

```{comment}
The result is `r myoutput`
```

Does that answer part of the need that lead to this issue ?

@cderv cderv added the theme: knitr concerns knitr package label Jan 11, 2022
@sbalci
Copy link

sbalci commented Jan 13, 2022

Does this engine use echo=FALSE as well?

@cderv
Copy link
Collaborator

cderv commented Jan 14, 2022

This engine will just make knitr ignore the content. Nothing will be output, no source chunk no result chunk.
So yes same result as with echo = FALSE - no source chunk in the output but no need to set options.

@sbalci
Copy link

sbalci commented Jan 14, 2022

perfect :) Thank you :)

@maxheld83
Copy link
Contributor Author

@cderv

Does that answer part of the need that lead to this issue ?

Yes! 🙏

I'll then tentatively close this ticket.

If users (such as myself in the above) want to comment something, there's a way to do that.

I was initially a bit surprised by the above behavior: that R would still be evaluated inside HTML comment tags, which, given the tag hierarchy, seemed surprising (since the comment tag is above whatever is inside).

But, now I'm not sure anymore, since (AFAIK), rmarkdown always first runs knitr toresolve all chunks, and only then calls pandoc, or whatever else to evaluate the markup.
So, from that perspective, it makes perfect sense that R inside HTML comment tags still gets evaluated by rmarkdown/knitr..
knitr never really cares nor knows about the HTML context in which some chunk is rendered, so it makes sense that it wouldn't even know/care about an HTML comment tag.

@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 19, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature a feature request or enhancement theme: knitr concerns knitr package
Projects
None yet
Development

No branches or pull requests

4 participants