Rmarkdown knitr doesn't process code chunks when generating preview or html doc (and only shows code literals)

Apologies if I'm using the wrong or incorrect lingo/terminology, but this is exactly the same issue reported in this issue from 1.5 months ago:

For some unknown reason, the knit function isn't processing the rcode snippets when generating a preview or the html document, even when using the stub/default text. For some unknown reason, the original poster for the previous topic never followed up, but I'm able to generate the html file correctly if I manually specify

rmarkdown::render( file_name )

The specific steps to generate the issue are:

  1. create new R markdown file, using File -> New File -> R Markdown
  2. Press ctrl-shift-k (or select File -> Knit Document)
  3. save html doc to whatever name you want (I chose dbg.html)

the generated html document won't contain the result of the r code, but will just contain the string literals, i.e.

{r cars} summary(cars)

This seems like an issue in how the code chunk is seen.
Can you open the document in the visual editor of RStudio IDE and check that it is seen as a code chunk ?


If you mean the raw Rmd code, here's a screenshot of it. I believe that the visual editor is recognizing that it's R code:

Unfortunately, I can't share the html file that gets generated, as I can't share html files, but here's a link to a copy on dropbox: https://www.dropbox.com/s/8jt8rylqv56gpmz/dbg.html?dl=0

(viewing allowed, but not editting)

Also, this is one of the relevant code snippets from the html file:

<p><code>{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE)</code></p>
<div id="r-markdown" class="section level2">
<h2>R Markdown</h2>
<p>This is an R Markdown document. Markdown is a simple formatting
syntax for authoring HTML, PDF, and MS Word documents. For more details
on using R Markdown see <a href="http://rmarkdown.rstudio.com" class="uri">http://rmarkdown.rstudio.com</a>.</p>
<p>When you click the <strong>Knit</strong> button a document will be
generated that includes both content as well as the output of any
embedded R code chunks within the document. You can embed an R code
chunk like this:</p>
<p><code>{r cars} summary(cars)</code></p>
<div id="including-plots" class="section level2">
<h2>Including Plots</h2>
<p>You can also embed plots, for example:</p>
<p><code>{r pressure, echo=FALSE} plot(pressure)</code></p>
<p>Note that the <code>echo = FALSE</code> parameter was added to the
code chunk to prevent printing of the R code that generated the

So, it looks like it might be getting marked up correctly (?)

In terms of versions, I'm using:
R version: 4.2.0 (2022-04-22)
R studio version: 2022.02.2 build 485

Unfortunately, I can't reproduce using last version of everything.

I meant clicking on the button "visual" next to "source" to activate the visual editor mode.

Also can you save the document ? I see its is called Untitled2.

Somehow the chunks are not correctly processed.

Other hints:

Can you try rendering using rmarkdown::render() function in your R console to see if you get the same results ?

Other hint:
Can you update to latest RStudio version or even try a daily version https://dailies.rstudio.com/ to test if the IDE is related to your issue.

Hope it helps

Sure, here's what it looks like in visual mode (apologies, I'd missed that option in the window):

From what it looks like, it recognizes the code chunks as code chunks, but doesn't automatically execute the code, but give me the ability to manually execute it.

Here's a before image:

And here's an image after I manually execute one of the chunks:

I checked to see if there was a settings for automatically executing the code, and found this previous post, but it looks like the settings are set correctly in my Rstudio instance.

link to previous post: https://community.rstudio.com/t/automatic-execution-of-setup-chunk-in-rmd/71483/3

All the previous screenshots were with the default Rstudio version.

As requested, I've installed the latest daily (version 2022.06.0-daily+418)

I was getting the same behavior with the daily, but I realize now that it was an issue of the order of operations.

If I save it as .Rmd file (dbg.Rmd), and THEN knit the document, it produces an html file (dbg.html) that contains the output from the R code as expected.

However, if I do what I was doing initially, and knit the document before saving it as an .Rmd file, I'm given a choice of saving the file. If I opt to save it as dbg.html, Rstudio doesn't process the embedded R code chunks when it saves the file. (However, if I save it as an .Rmd when I choose to knit the unsaved doc, the html file is produced as expected (containing the results of the code chunks)).

This makes sense now that think of it, but obviously, I was confused about the correct process to render the R markdown.

Oh that is why. This is because knitr is seing a HTML file as source, and it won't process the code chunk syntax used as markdown. For knitting a HTML file, there is another syntax. But .Rmd is the recommended way.

So conclusion: the file to render needs to be a .Rmd file.

If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.