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

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
```

Related

Assign figure caption to html widget (vtree package) in R markdown output

I need to implement a figure caption in a plot that is generated by the vtree package in R markdown. I learned that this is a htmlwidget and figure captions should now be possible for htmlwidgets used in R markdown with install.packages('webshot') and webshot::install_phantomjs() (reference: https://bookdown.org/yihui/bookdown/html-widgets.html#ref-R-DT.
But days after I am not really any step further. I did not find any example (show case) for this issue (fig.cap for htmlwidgets in R markdown in the net) so my hope is that someone out there can give me some help!
In my iris dataset example, in Fig. 1 the caption is not working in contrast to Fig. 2.
my iris set example RMD file:
YAML
---
title: "test"
author: "TJ"
date: "14 12 2020"
output: html_document
---
code chunk 1: load libraries and data
knitr::opts_chunk$set(echo = TRUE)
library(vtree)
library(webshot)
library(tidyverse)
attach(iris)
df <- iris %>%
select(Species) %>%
cbind(sapply(levels(.$Species), `==`, .$Species))
code chunk 2: Figure 1
{r fig1, echo=FALSE, fig.cap="Vtree plot"}
vtree(iris, "Species")
code chunk 3: Figure 2
{r fig2, echo=FALSE, fig.cap="Scatter plot iris dataset"}
plot(Sepal.Length, Sepal.Width, main="Scatterplot Example",
xlab="Sepal Length ", ylab="Sepal Width ", pch=19)
There is a workaround using the Magick package.You save the image as .png using grVizToPNG (make sure you comment this line out before you render your document or put it in a separate chunk with ยด{r eval = FALSE}, otherwise you will get an error during rendering:
```{r eval=FALSE, echo = FALSE}
myimage <- vtree(iris, "Species")
saveMyimage <- grVizToPNG(myimage, width=800)
```
Here you use the Magickpackage:
```{r magick, echo= FALSE}
MyimagePNG <- image_read("myimage.png")
image_annotate(MyimagePNG, "Vtree plot", size = 35, gravity = "southwest")
```

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)
```

How to type tilde in formulas in RMarkdown

I am trying to type equations on RMarkdown to create a PDF and I want to use a tilde, how can I go about it. I am trying this but throws back a pandoc.exe: Error producing PDF from TeX source ....
---
title: "See"
date: "24 September 2016"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
follows a Bernouli distribution
$$y_i \~ Bernouli(p_i)$$
I believe if you do
$$
\sim
$$
This should produce the ~ you are attempting to place in your formula.

Cannot create PDF from R Markdown without template file lines

I am trying to create a long document in R Studio using R Markdown to PDF, and I am getting the following error when I push the "Knit PDF" button:
! Undefined control sequence.
l.124 \begin{center}\includegraphics
pandoc.exe: Error producing PDF from TeX source
Error: pandoc document conversion failed with error 43
In addition: There were 50 or more warnings (use warnings() to see the first 50)
Execution halted
I am able to properly create the PDF if I make a new R Markdown document and I paste the code in from the original, provided I keep the following code at the end of the new document. When I erase this code, I get the above error.
```{r, echo=FALSE}
plot(cars)
```
I have no need or desire for the cars plot to be in my document. I am able to Knit this document into HTML without the undesired code, but not to PDF. Can anyone advise why this error may be happening? (This is my first post to this forum, so if you need more specific information, please advise).
I am running Windows 7 32 bit, R Studio version 0.98.1103, MiKTeX version 2.9.
Edit/Update: Here is my code, including the cars graph which I do not want.
---
title: "Untitled"
date: "Saturday, April 25, 2015"
output: pdf_document
---
```{r echo=FALSE, warning=FALSE, include=FALSE}
require(qcc)
require(data.table)
require(ggplot2)
require(sm)
require(knitr)
require(xtable)
attach(airquality)
airquality$indicator <- ifelse(airquality$Month < 8,'Classic','New')
```
```{r echo=FALSE, warning=FALSE, results='hide', fig.align='center'}
par(mfrow=c(2,1), oma=c(1,1,1,1), cex=.5, mex=.5, ps=8, mgp=c(3,1,0))
airquality <- airquality[complete.cases(airquality),]
# Plot variable on an SPC chart of type xbar.one
imrchart2 <- qcc(airquality$Ozone[airquality$indicator=="Classic"], "xbar.one", std.dev="SD", add.stats=TRUE, ylab="Ozone", title="Xbar.One Chart for Ozone", xlab="", data.name="Classic", restore.par=FALSE, newdata=airquality$Ozone[airquality$indicator=="New"], newdata.name="New", ylim=range(airquality$Ozone))
# Plot a CUSUM chart
cusum(airquality$Ozone[airquality$indicator=="Classic"], sizes=1, add.stats=TRUE, ylab="Ozone", title="CUSUM Chart for Ozone", xlab="", restore.par=FALSE, newdata=airquality$Ozone[airquality$indicator=="New"], data.name="Classic", newdata.name="New")
```
```{r, echo=FALSE}
plot(cars)
```

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

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

Resources