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:
create new R markdown file, using File -> New File -> R Markdown
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 ?
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>
<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
plot.</p>
</div>
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.
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.
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.
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: