I have a .Rnw file that looks like this:
\documentclass[a4paper,11pt]{article}
\begin{document}
\title{}
\author{me}
\date{\today}
\maketitle
\section{Header}
<<table_mtcars, results = "asis">>=
stargazer(rbind(mtcars, mtcars, mtcars), summary = F)
#
\FloatBarrier
\end{document}
I can use knitr::knit to produce a .tex file and then convert this to a PDF. The resultant PDF looks like this:
Note how the table disappears off the end of page 2 and doesnt resurface on page 3. How can I get the table to continue on page 3 of the PDF using stargazer?
As far as I know this is not currently supported by stargazer. I would recommend emailing the developer, who is usually very open to suggestions. The issue is that you need to use LaTeX's longtable environment instead of tabular, but this is the part that Stargazer doesn't seem to support.
You can create a workaround by doing:
\documentclass[a4paper,11pt]{article}
\usepackage{longtable}
\begin{document}
\title{}
\author{me}
\date{\today}
\maketitle
\section{Header}
\Sexpr{library(knitr);gsub('tabular','longtable',knit(text="<<results = 'asis'>>=\nlibrary(stargazer)\nstargazer(rbind(mtcars, mtcars, mtcars), summary = FALSE, float = FALSE)\n#"))}
\end{document}
Basically, you're calling knitr within your .Rnw file in order to build the table, and gsub the tabular environment to longtable. When I run this, it comes out as three pages long.
Related
I just found this awesome technique to put the code used in the .Rmd file in the appendix (of that same file).
However, I am using R Sweave and not R Markdown and I would like to know if there exists a similar way to put all the code at the end in a unique chunk. The code to do that in Markdown does not work in Sweave. I precise that, unlike this post, I do not have a separate .R file where the calculations are made. Everything is done in the .Rnw file.
Does anybody know how to do it?
Edit : a reproducible example
\documentclass[11pt, twocolumn]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\begin{document}
\SweaveOpts{concordance=TRUE}
<<reg2, echo=FALSE, print=FALSE>>=
head(mtcars)
#
<<reg3, echo=FALSE, print=FALSE>>=
head(iris)
#
\section*{Appendix}
% the place where I could like to put the whole code
\end{document}
This chunk works to include the code:
<<echo=FALSE, eval=TRUE>>=
filename <- tempfile(fileext=".R")
Stangle("test.Rnw", output = filename, quiet = TRUE)
cat(readLines(filename), sep = "\n")
#
When I include that in your example file, I see this:
I think it's possible to modify the format a bit; see ?Rtangle for some details. Similar things are possible with knitr, but it's more flexible. I suspect the best method would be similar to the one you found for RMarkdown.
I'm using .RNW file from knitr (RStudio, Rversion 3.1) and would like to get the output in a pdf file in the same order as the chunks are in the .RNW file. I'm not writing an article but am analyzing data producing a lot of output.
As I'm writing a little text between the chunks the pdf file is messed up to optimize information (not too many empty spaces) on the pages.
Is there a option available to force the print order?
Thanks in advance,
Regards, Hans
Here is the code I'm using
<<summary2, echo=FALSE, results=tex>>=
library(xtable)
attach(dataVoor)
tab <- table(PathEM,NTpathEm)
xtable(tab, caption = c("Relatie path EM en NT-path EM"))
... several tab and xtable as above in the same chunck....
detach(dataVoor)
#
\subsection*{Analyse van risicofactoren voor infectie NA spenen}
<<ReadData2, echo=FALSE>>=
dataNa <- read.delim("~/datasetNa.txt")
#
<<summaryNa2, echo=FALSE, results=verbatim>>=
attach(dataNa)
names(dataNa)
summary(dataNa)
#
So several xtables are placed in between the summary of dataNA
I did find some solution by specifying argument table.placement in the print function of xtable:
print(xtable(tab, digits=0, caption = c("Frequency table of Gender per Zoba")), caption.placement="top", table.placement = getOption("xtable.table.placement", "h!"))
Now the results in the pdf are in the same order as in the program was specified.
Thanks for replying on my question.
I am sorry, I am new to using knitr to make slides. I generally use the latex() function in Hmisc package to generate my tables based on R objects. I would like to produce a slide that shows the r code and then below it displays the properly formatted table. Something like:
``` {r}
latex(tabdat,file="tables/tabdat.tex",ctable=TRUE,caption="A basic table",caption.loc="bottom",label="tab:dat1",row.names=NULL,rowlabel="")
```
So that the finished slide displays the exact r code and the formatted table looking exactly as if I had run latex using \input{tabdat}
I would appreciate any advice on how to accomplish this.
Thanks!
I am a bit puzzled because you talk about PDF/LaTeX output but you are using R markdown tags.
Here are small examples for both cases, R Sweave, i.e. LaTeX output, and R markdown, i.e. HTML output. For creating the LaTeX code there are several packages available (xtable, Hmisc etc.) for HTML AFAIK only xtable.
The main point of how to include the raw output just like it appears in the console is the same for both output types and was already explained by Tyler Rinker above, i.e. by adding results="asis" to the chunk options.
PDF/LaTeX / Rnw-file
\documentclass{article}
\begin{document}
<<echo=FALSE, message=FALSE>>=
library(Hmisc)
library(xtable)
#
<<results='asis'>>=
latex(head(iris), file = '')
#
<<results='asis'>>=
xtable(head(iris))
#
\end{document}
HTML, Rmd-file
```{r echo=FALSE}
library(xtable)
```
```{r results='asis'}
tbl <- xtable(head(iris))
print(tbl, type="html")
```
Look here for more examples and options: http://www.stat.iastate.edu/centers/CCGS/slides/slides-Rtables.pdf
My converting the R script (.R) into an RMarkdown file (.rmd) in RStudio and then pressing “knit html” result in two output files (that is, .html and .md files). I am confronting 2 issues:
The html file shows that the title of the ggplot graph gets chopped. I have changed the original width of 11 to a new width of 15:
ggsave(file=outFile, width=15, height=7)
How would I resolve the issue ? And how would I convert the .md file into a PDF file ?
Your question is not exactly clear. I'm not sure, for example, why you are using ggsave() to begin with. You can directly create a "ggplot" image in your file to knit and set your figure width and height in your input file.
In a ".Rmd" file, your code might look something like:
```{r fig.width=7, fig.height=4, echo=FALSE}
library(ggplot2)
qplot(mpg, wt, data=mtcars)
```
The echo=FALSE will make it so that the code doesn't display, but the resulting plot will. The figure width and height have been set with the relevant arguments.
If you wanted to convert your resulting markdown file to PDF, I would recommend looking at Pandoc which will allow you to do something like the following to convert your file to a PDF:
pandoc infile.md -o outfile.pdf
Alternatively, you can use R Sweave instead of R Markdown in R/RStudio. For instance, if you create a new "Rnw" file in RStudio and paste the following in, you'll have the option to directly compile a PDF instead of compile an HTML.
\documentclass{article}
\begin{document}
<<fig.width=5, fig.height=3, echo=FALSE>>=
library(ggplot2)
qplot(mpg, wt, data=mtcars)
#
\end{document}
I'm using Sweave and latex() from the Hmisc package to insert a longtable in my PDF.
When I do it the first time, the table is spread nicely, filling up the pages with the table.
If I do it again, some pages are only half full (like page 4 of PDF), which looks weird and somehow wrong because it seems to be unnecessary space.
Is there a way to control this? Or what could I do to improve the look?
Especially, if I'm adding some text and graphs, it won't look good with the empty space on page 4.
\documentclass{article}
\usepackage{Sweave}
\usepackage{longtable}
\usepackage{booktabs}
\begin{document}
\SweaveOpts{concordance=TRUE}
I want to insert this longtable
<<tab.R,echo=FALSE,results=tex>>=
library(Hmisc)
#library(xtable)
x <- matrix(rnorm(1000), ncol = 10)
x.big <- data.frame(x)
latex(x.big,"",file="",longtable=TRUE, dec=2,caption='First longtable spanning several pages')
#
then write some text. Maybe add a graph...
And then another table
<<tab.R,echo=FALSE,results=tex>>=
latex(x.big,"",file="",longtable=TRUE, dec=2,caption='Second longtable spanning wrongly')
#
\end{document}
Don't pass this question over to the latex group, this is a problem of Hmisc/latex that adds a \clearpage into tex every 40 lines by default. Check parameter lines.page=40 of latex. I do not understand why this default has been set, but something like
latex(x.big,"",file="",longtable=TRUE, dec=2,
caption='Second longtable spanning wrongly', lines.page=4000)
gets you around the problem.