R notebook stops printing graphs inline - r

RStudio starts printing graphs to console instead of inline
When I start RStudio R notebook or markdown, I can print graphs (e.g. ggplot2) inline. However, as I start adding more code to the window with tabular output (which shows inline) and then try to print the graph, it begins printing into the console instead of inline. I am not changing the html setup and the original default setup stays the same. When I run Preview Notebook, only the code shows without any output, tabular or graph. When I knit html, both the code and the output (tabular and graph) show.
I cannot detect why RStudio switches from inline to console for graphs, anyone has experience with this? I know it is not specific to the R code (I am using R 3.6.0) since those same charts worked before inline and they show inline on a fresh Notebook / Markdown.

I discovered a workaround:
editor_options:
chunk_output_type: inline
This should not be required because the Notebooks' default is inline and I would not change it. However, this code seems to have fixed the issue.

Related

How do I get code highlights and autocomplete for Latex code in R Markdown in VSCode?

I am writing an .Rmd file in VSCode using the VSCode R extension. The extension does a great job of highlighting and code completing r code inside code chunks. However, it does not highlight, or code complete, inline Latex code.
For example, this is how my inline Latex code looks like:
Now, if I change the language mode from R Markdown to Markdown as indicated in this question VS Code Latex Syntax in Markdown, then I get great Latex code highlighting and completion, as shown below:
However, when I do this I loose all highlights and completion for the r code inside the code chunks.
How do I set things up so that I have highlights and code completion for code chunks and inline Latex code?

Can't knit in R markdown

Recently I was doing a course in Coursera and faced the problem while doing R markdown. When I run the code in the console it works just fine however, when I knit it, the rmarkdown only show text in the code chunk as below.
I run this in console and it work just fine:
But when i knit the code appear as text:
When I hit the green arrow it appears like there are some error going on:
The bottom screenshot you provided is not an R code chunk. It does not have the proper header. R code chunks in an R markdown file must be formatted like this:
```{r}
R code goes here
```

render SQL in Rmarkdown notebook, not just when knit

If I have an R block in an Rmarkdown notebook, the code is visible in the rendered nb.html file. However, if I have a SQL block, I can't find a way to make the code visible in the rendered file.
However, if I generate an html_document (standard knitr) instead of an html_notebook, the rendered HTML does include the SQL code (and output).
Is there a way to get the notebook output to do something closer to what the knit output does?
I've also encountered this recently, Harlan, and began by asking if there's a way to get notebook output more like knit output.
I then wondered if there was a way to get knit output more like notebook output and realised that there was.
After asking the folks at RStudio, I learnt that you can add the following line to the YAML options in order to generate a html_document that, when rendered, enables the RMD to be downloaded. Specifically, use:
output:
html_document:
code_download: true
I now use that in all of my code and it works well. I hope it helps you, too.

Cannot shut off SweaveOpts Concordance = True [duplicate]

This question already has an answer here:
r knitr chunk options for figure height / width are not working
(1 answer)
Closed 7 years ago.
UPDATE
SweaveOpts are now shut off and the macro is no longer auto populating, but I still cannot get my plot into the document
I am using R-Studio, the latest version on my laptop and my desktop. All my packages are up-to-date as of this morning. I am trying to on my laptop insert a plot into a pdf. I am using knitr and pdfLatex, the global options in R-Studio are set to knitr and pdfLatex, the same as on my desktop.
When trying this on my laptop the following always appears in my .Rnw file:
\SweaveOpts{concordance=TRUE}
which is preventing me from rendering my pdf the way I want, meaning my plots do not show inside the document but as a separate file. I ensured I have exactly the same settings in R-studio on the laptop and desktop, I am using the same exact version of R in each 3.1.3 and the same exact packages to produce the document knitr and ggplot2
I am completely baffled on how to shut \SweaveOpts{concordance=TRUE} off and make it stay off.
The head of the document on my desktop looks like this:
\documentclass{article}
\begin{document}
\begin{titepage}
\begin{center}
...
And on my laptop for a different document
\documentclass{article}
\usepackage{setspace}
\begin{document}
\begin{title}
\begin{center}
...
I did get rid of the \usepackage{setspace} line just to see if that was the issue and it was not.
I have also noticed that when I insert a code chunk and start to type options inside of it, like fig.align='center' the autocomplete works on the desktop but not the laptop, almost like it does not recognize that the knitr library is loaded.
Try the menu
Tools -> Global Options -> Sweave
and unselect Always enable Rnw concordance (required for Synctex) which should take care of it.

Manually use R Knit/Markdown to produce plots for HTML

I am using knit()and markdownToHTML() to automatically generate reports.
The issue is that I am not outputting plots when using these commands. However, when I use RStudio's Knit HTML button, the plots get generated. When I then use my own knit/markdown function, it suddenly outputs the plot. When I switch to another document and knit that one, the old plot appears.
Example:
```{r figA, result='asis', echo=TRUE, dpi=300, out.width="600px",
fig=TRUE, fig.align='center', fig.path="figure/"}
plot(1:10)
```
Using commands:
knit(rmd, md, quiet=TRUE)
markdownToHTML(md, html, stylesheet=style)
So I guess there are 2 questions, depending on how you want to approach it:
What magic is going on in Rstudio's Knit HTML?
How can I produce/include without depending on RStudio's Knit HTML button?
The only issue I see here is that this doesn't work when you have the chunk options {...} spanning two lines. If it's all on one line, it works fine. Am I missing something?
See how this is not allowed under knitr in the documentation:
Chunk options must be written in one line; no line breaks are allowed inside chunk options;
RStudio must handle linebreaks in a non-standard way.
This is really embarrassing, I really thought I read the documentation carefully:
include: (TRUE; logical) whether to include the chunk output in the
final output document; if include=FALSE, nothing will be written into
the output document, but the code is still evaluated and plot files
are generated if there are any plots in the chunk, so you can manually
insert figures; note this is the only chunk option that is not cached,
i.e., changing it will not invalidate the cache
Simply adding {..., include=TRUE} did the trick. I would say it would be a pretty sensible default though.

Resources