allowframebreaks in beamer shading box disappear using knitr - r

I have the following code
\documentclass{beamer}
\begin{document}
<<setup, include=FALSE>>=
opts_chunk$set(out.lines = 4)
opts_chunk$set(size = 'footnotesize')
options(width=60)
knit_hooks$set(document = function(x) {
gsub('\\\\(begin|end)\\{kframe\\}', '', x)
})
#
After I do this, the R source code and output which is supposing to be displayed in a shaded box now disappear. Any idea on how to get the shaded box back?

Related

I have a for loop of qplots, how can you structure the plots individually using LaTeX code e.g adding captions to the plots

\begin{figure}[h]
\centering
<<echo=FALSE>>=
for(i in 2:38){
print(my_plot_function(i))
}
#
\end{figure}
This is my code, but what is happening is that when I compile my PDF I only get the first two plots that fit on the first page and I do not get the rest of the plots.
I would like to have all of the plots on separate pages.
And how would I go about adding captions to each individual plot in the for loop.
\documentclass{article}
\begin{document}
<<echo = FALSE, fig.cap = c("First, Second, Third"), results = "asis">>=
library(ggplot2)
for (i in 1:3) {
print(qplot(x = 1, y = i))
cat("Arbitrary \\LaTeX code! \\clearpage")
}
#
\end{document}
You can use the chunk option fig.cap to add captions to your plots. Note that this will wrap your figure in a figure environment, turning it into a float.
Use the chunk option results="asis" to be able to print arbitrary text (including LaTeX markup) into your document using cat().

Unable to compile Plotly charts from R sweave to pdf

I'm unable to convert charts created by plotly in R sweave to Pdf. But other charts can be created. Is there any other additional code needs to be added for plotly charts?
Below is an example
\documentclass{article}
\begin{document}
\SweaveOpts{concordance=TRUE}
<<echo = FALSE>>=
C1<-c("A","B","C")
C2<-c(10,20,30)
Df<-data.frame(C1,C2)
#
\begin {figure}
<<echo = FALSE, fig = TRUE>>=
suppressWarnings (library (plotly))
plot_ly(Df, x = ~C1, y = ~C2, type = "bar")%>% layout(title = "ABC")
#
\caption {PlotlyChart}
\end {figure}
Normal Chart
\begin {figure}
<<echo = FALSE, fig = TRUE>>=
barplot(Df[ ,2], names.arg = Df[ ,1])
#
\caption {Normal_Chart}
\end {figure}
\end{document}
When I try a normal box plot in R, it works. Only when I add Plotly chart, I get an error message as
Running pdflatex on ABC.tex...failed
Error running /Library/TeX/texbin/pdflatex (exit code 1)
I'm new to R sweave and Latex, so any help is appreciated. Thanks!
Plotly produces javascript code that draws the plot. This code is to be executed within a web browser. Plotly is usable when generating an HTML document only. No PDF, no Latex.
BUT you can use package webshot with phantomjs to save the plotly as an image, then include the image as a figure.
First install webshot:
install.packages("webshot")
webshot::install_phantomjs()
Then modify you Sweave file like this:
\documentclass{article}
\begin{document}
\SweaveOpts{concordance=TRUE}
<<echo = FALSE>>=
C1<-c("A","B","C")
C2<-c(10,20,30)
Df<-data.frame(C1,C2)
#
<<echo = FALSE, fig = FALSE>>=
suppressWarnings (library (webshot))
suppressWarnings (library (plotly))
py <- plot_ly(Df, x = ~C1, y = ~C2, type = "bar")%>% layout(title = "ABC")
export(py, file = "myplot.png")
#
\begin {figure}
\includegraphics{myplot.png}
\end {figure}
\end{document}

RMarkdown: Long text in code chunks with multiple graphics

I am preparing a R markdown document to plot some graphics and some short text accompanying each graphic. The graphics are generated in a code chunk within a loop.
Right now, it looks like this:
First, all the text is outputted, then the graphics. I would like this pattern:
text
graphic
text
graphic
text
graphic
Also, I don't know how to apply line breaks in the cat() output. This is needed to make the whole label visible.
Any ideas on these problems?
This is my code:
```{r, echo = FALSE, comment='', fig.width=8, fig.height=3, fig.show='hold', fig.align='center'}
# vars is a data.frame with some variables names
for (i in 1:nrow(vars)){
options(warn=-1)
label <- 'some long text'
cat(label)
capture.output( data <- analysisFunction(name) )
capture.output( plottingFunctionWithGGplot2(data) )
cat('\n\n')
}
```
Thanks.

Knitr: Turning the R chunk figure caption 90 degrees inline Latex

Here is my a code to demo what I cannot do. I use an R chunk to make a figure and use out.extra='angle=90' to make the figure post sideways on the page. Now how do I get my caption to go sideways too?
\documentclass{article}
\usepackage{subcaption}
\begin{document}
<<test-plot,echo=FALSE,fig.cap="I would like to be posted sideways under figure, NOT HERE",out.extra='angle=90'>>=
plot(1)
abline(0, 1)
plot(rnorm(10))
for(i in 1:10) {
abline(v = i, lty = 2)
}
#
\end{document}

Knitr emptyline after \include graphics

I have a little question, I'm not sure of the cause, but the following knitr code :
<<toto, echo=FALSE, comment=NA, results='asis'>>=
#Data
a<- c(1:100)
b<- c(100:200)
#Plot1
plot(a)
#Plot2
plot(b)
#
produce this LaTeX code :
\includegraphics[width=\maxwidth]{figure/toto1}
(empty line here)
\includegraphics[width=\maxwidth]{figure/toto2}
(empty line here)
How can I remove this empty line?
Use the chunk option fig.show='hold'.

Resources