R markdown: Knitr: histogram dows not show when document titles are present - r

I have been battling with a curious problem and would appreciate if someone can help me. I am using the latest version of R Studio and packages.
---
title: "XYZ"
author: "ACB"
date: "Tuesday, October 21, 2014"
output: pdf_document
---
typing `r data(mtcars)`.
Some text
```{r}
mtcars$am <- as.factor(mtcars$am)
levels(mtcars$am) <-c("Automatic", "Manual")
```
Some text
```{r fig.align='center', fig.height=4}
hist(mtcars$mpg[mtcars$am=="Automatic"], breaks=12, main="mpg for automatic vehicles", xlab="mpg", xlim=c(10, 35))
```
If I Knit the above to PDF the histogram does not show; if the output is to HTML it works.
Thanks

Related

Linking two code blocks together so that their output appears under the same category in the markdown

I've got a couple of charts that show two related data sets. I would like to have them appear in the same 'category' on the markdown, but under two separate 'subtitles' on the table of contents.
My YAML follows this pattern (though I have also used "toc:true" and "toc_float: true" previously):
---
title: "Update"
author: "Me"
date: "`r Sys.Date()`"
output:
rmdformats::readthedown:
fig_width: 12
fig_height: 5
---
The code I use to produce the charts in the markdown follows this format:
# Time-series
## Nominal
```{r chart1,echo=FALSE, cache=FALSE, warning=FALSE, message=FALSE, fig.height=25,fig.width=10}
Chart1
```
## Real
```{r chart2,echo=FALSE, cache=FALSE, warning=FALSE, message=FALSE, fig.height=25,fig.width=10}
Chart2
```
The 'category title' (Time-series) and 'subtitle' (Nominal) work as intended, and appear linked in the table of contents when the markdown is produced. The second 'subtitle' (Real) appears unlinked, and while it seems to recognise that 'Real' is intended to be a subtitle in RStudio, when the markdown html is produced it neither recognizes as a subtitle nor does it link it to the 'category title' above. Does anyone how to achieve this?
you are missing a blank line between chunk1 and ## Real
---
title: "Untitled"
author: "domingo"
date: "11 2 2022"
output:
html_document:
toc: TRUE
toc_float: TRUE
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Time-series
## Nominal
```{r chart1,echo=FALSE, cache=FALSE, warning=FALSE, message=FALSE, fig.height=25,fig.width=10}
plot(mtcars)
```
## Real
```{r chart2,echo=FALSE, cache=FALSE, warning=FALSE, message=FALSE, fig.height=25,fig.width=10}
plot(mtcars)
```

Render ggplotly() in R Markdown github_document

Interactive graphs created with ggplotly() go along nicely with html_document output in R Markdown, see eg. RMarkdown and ggplotly. For github_document output, however, the knitted HTML preview file does not show ggplotly() graphs.
I adopted the code from the linked SO post and only changed the output format in the header. Does anyone know how to render plotly graphs correctly with this kind of output? Or at least, if that is even possible?
---
title: "RmarkdownExample"
author: "fdetsch"
date: "April 16, 2020"
output: github_document
---
Here is the graph I generated.
```{r setup, message = FALSE, echo = FALSE, warning=FALSE}
# Require
library(plotly)
# Create
dsamp <- diamonds[sample(nrow(diamonds), 1000), ]
# Plot
g <- qplot(carat, price, data=dsamp, colour=clarity)
# Call
ggplotly(g)
```
For output: github_document, I found a workaround that renders ggplotly() graphs nicely using iframes. The trick is to export the plotly widget as HTML and subsequently embed it as iframe. In my opinion, the advantage over output: html_document with keep_md enabled is that the online .md file simply prints a link to the intermediary HTML file instead of the full widget code, making it much tidier.
---
title: "Render `ggplotly()` graphs in `github_document`"
author: "fdetsch"
date: "`r Sys.Date()`"
output: github_document
---
Here is the graph I generated.
```{r setup, message = FALSE, echo = FALSE, warning = FALSE}
# Require
library(plotly)
# Create
dsamp <- diamonds[sample(nrow(diamonds), 1000), ]
# Plot
g <- qplot(carat, price, data=dsamp, colour=clarity)
# Call
p <- ggplotly(g)
```
```{r include, echo = FALSE}
htmlwidgets::saveWidget(p, "index.html")
htmltools::tags$iframe(
src=file.path(getwd(), "index.html"),
width="100%",
height="600",
scrolling="no",
seamless="seamless",
frameBorder="0"
)
```
At least when opening the preview HTML in an external viewer, the interactive graph shows up. The RStudio (preview version 1.3.938) viewer currently fails to render the image.
There seem to be some problems with github_document, see here. My workaround: knit to html_document and save the resulting *.md-file.
So the YAML header is:
---
title: "RmarkdownExample"
author: "fdetsch"
date: "April 16, 2020"
output:
html_document:
keep_md: true
---
You can then use the md file to upload to github.

How to use RStudio to write R Markdown files with Stata graphs?

The code below works for Stata commands and output but does not produce the graphs:
---
title: "Stata and R Markdown (Windows)"
author: "Doug Hemken"
date: "July 2015"
output:
html_document:
toc: yes
---
```{r, echo=FALSE, message=FALSE}
require(knitr)
statapath <- "/Applications/Stata/StataSE.app/Contents/MacOS/stata-se"
opts_chunk$set(engine="stata", engine.path=statapath, comment="")
```
### Descriptive Statistics
A simple example.
```{r}
sysuse auto
summarize
```
```{r}
sysuse auto
twoway (scatter mpg weight)
```
Note that this is a follow-up of this question:
How to use RStudio to write a RMarkdown file with Stata commands?.
You need to export the graph and include an image link in the R Markdown as follows:
```{stata, echo=1, results="hide"}
twoway (scatter mpg weight)
graph export "myscatter.svg", replace
```

How do I produce a plot in landscape [rotated by 90 degrees] orientation in knitr?

The following knitr code give me the plot below -- how do I plot it in a landscape orientation?
```{r}
rm(list=ls())
library(tree)
set.seed(1111)
x1<-runif(100)
x2<-rnorm(100,mean=.3)
x3<-runif(100)
d1<-x1>0.5
d2<-x2>0.7
d3<-x3<0.2
y<-ifelse(d1,1,ifelse(d2,2,ifelse(d3,3,4)))
df<-data.frame(x1,x2,x3,y)
tr<-tree(y~.,data=df)
plot(tr)
text(tr)
```
If your want a pdf/LaTeX output it is quite easy with out.extra='angle=90' chunk argument :
---
title: "Rotation test"
output: pdf_document
---
```{r, out.extra='angle=90'}
rm(list=ls())
library(tree)
set.seed(1111)
x1<-runif(100)
x2<-rnorm(100,mean=.3)
x3<-runif(100)
d1<-x1>0.5
d2<-x2>0.7
d3<-x3<0.2
y<-ifelse(d1,1,ifelse(d2,2,ifelse(d3,3,4)))
df<-data.frame(x1,x2,x3,y)
tr<-tree(y~.,data=df)
plot(tr)
text(tr)
```
In some circumnstances it is better to keep the graph as it but to rotate just one page in landscape format within you document.
You need pdflscape LaTeX package for this (included for example in the texlive-latex-base package in Ubuntu as "oberdiek").
In the following example the graph is extended to occupy a full A4 page in landscape format. NB : you must specify fig.align='center' to make it work.
---
title: "Rotation test"
output: pdf_document
header-includes:
- \usepackage{pdflscape}
---
```{r}
rm(list=ls())
library(tree)
set.seed(1111)
x1<-runif(100)
x2<-rnorm(100,mean=.3)
x3<-runif(100)
d1<-x1>0.5
d2<-x2>0.7
d3<-x3<0.2
y<-ifelse(d1,1,ifelse(d2,2,ifelse(d3,3,4)))
df<-data.frame(x1,x2,x3,y)
tr<-tree(y~.,data=df)
```
\newpage
\begin{landscape}
```{r fig.align='center', fig.width = 27/2.54, fig.height = 19/2.54}
plot(tr)
text(tr)
```
\end{landscape}
```{r}
summary(tr)
```

R Markdown Presentation not loading/ rendering interactive Plotly chart

I am using Plotly with R to create a chart that will be rendered in a R Markdown Presentation With Ioslides, but instead of showing the demo chart from the website like the following:
It is rendering the steps like this:
My code is pretty simple:
---
title: "R Markdown Presentation & Plotly"
author: "Eduardo Almeida"
date: "February 19, 2017"
output: ioslides_presentation
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
## Interactive plot with Plotly
```{r}
library(plotly)
p <- plot_ly(economics, x = ~date, y = ~unemploy / pop)
```
As Karthik Arumugham pointed out you need to display the plot, either by entering p or not assigning plot_ly to variable but calling it directly.
I'd suggest to explicitly state the missing variables (type='scatter', mode='markers') instead of suppressing the output messages. In addition you could add {r, warning=F} to get rid of the
Error: attempt to use zero-length variable name
message.
---
title: "R Markdown Presentation & Plotly"
author: "Eduardo Almeida"
date: "February 19, 2017"
output: ioslides_presentation
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
## Interactive plot with Plotly
```{r, warning=F}
suppressPackageStartupMessages({library(plotly)})
library(plotly)
plot_ly(economics, x = ~date, y = ~unemploy / pop, type='scatter', mode='markers')
```

Resources