How to align table columns i PDF with `texreg` - r

The texreg package is supposed to write tables with nicely aligned columns, using the command dcolumn = TRUE, see the user's guide for the texreg package, dated 3 March 2018. The command calls the dcolumn package, but dcolumn is not available for the current version of R (see earlier post).
Without alignment, tables generated by texreg do not look good in a PDF document compiled with knitr. (The knitr package uses the Times New Roman font family for PDF, not monospaced font.) One (not optimal) solution might be to change the font in the whole PDF to monospace. I have found no reference to changing the font family in the knitr documentation, but a post here at SO discusses it. I had no success using the suggested solution. It would anyway not be a great solution to use monospace font throughout a document that combines text and code.
So, how should users of texreg make sure tables in a document knitted to PDF have aligned columns in a table?
This question is very similar to an earlier post, but no suggestion was provided to the earlier question. It is probably important for most users of texreg to have a solution for aligning table columns after the dcolumn package has become outdated.

Related

Fractions and Superscript in a table while knitting to docx (RMarkdown)

I have been using officedown for some time now to knit to docx. I encountered an issue where a table I had to create required superscripts in the table as shown below
This led me to go over all the methods for creating a table. Rather than typing it all out here I created an Rmd document below which goes through all the issues. I would be interested in knowing if anyone has any comments or suggestions. My goal is simple. We want a table that exports to docx that
Shows up in the list of tables
Has a caption
In-text reference
Shows the fractions correctly
Please find the detailed Rmd with examples HERE
I apologize for the length of the Rmd document but I wanted to showcase in detail what I have attempted to do to facilitate answers and suggestions
Update: Got a little roasted on the officer GitHub but it seems that fractions are not possible at the moment but subscripts are using ftExtra

Stargazer in a bookdown site

I'm trying to show Stargazer tables (both for regressions and data summary) in a bookdown based website. stargazer output does not look like it looks in outside bookdown (i.e markdown/knitr/html). It has spaces between rows, rows are zebra striped (abit like bootstrapped theme)
I believe that the style.css overrides stargazers' known table format, however I couldn't find any evidence to that, nor couldn't modified it by myself.
I'm sorry for asking without reprex, I find it a bit hard to make reprex to a book. Anyway, stargazer chunk is:
```{r ,results='asis'}
stargazer(lm1, type=`html`)
````
So to problem is that stargazer tables are wrapped again according to the styling in the "plugin-table.css" file that located in
"./_book/libs/gitbook-2.6.7/css/plugin-table.css".
I get rid from the Kable-like formating when I remove this file it after rendering the book with these command:
bookdown::render_book("index.Rmd","bookdown::gitbook", clean_envir = TRUE) # render to HTML
file.remove("./_book/libs/gitbook-2.6.7/css/plugin-table.css")
Of course, this is not the best practice, this solution hurts kableextra formating for example. However, as I tend to use mainly Stargazer, I don't find it that bad.

A workflow with bookdown to produce frequency, cross tables and model summary tables

I am starting to write a book using bookdown and trying to find the best workflow. First of all I am trying to make frequency tables, crosstables and model summaries, using bookdown with pdf format mainly, but I am pretty sure my advisor will love I send word documents for revision, so also getting a word output would be awesome. Word output can be achieved producing first the .html file and then opening with Word. So .html and .pdf output at the same time are desiderables.
Tables are the main problem because seems impossible to find a productive way to produce frequency, crosstable and summarymodels without too excessive pain in both formats at the same time. Using knitr has an added value since it and kableExtra provide a set of customizations.
So far I've tried:
1) sjmisc / sjPlot with the frq / sjt.frq (deprecated) function
but only provides html objects as it is said in the comments of this
post, also has the lack of labels for cross reference in the
document.
2) stargazer package but doesn't have frequency tables available and
.pdf and .html output at the same time seems impossible to achieve.
However summary models are pretty good!
3) descr package with the freq function used inside kable:
f <- descr::freq(iris$Species, plot = F)
kableExtra::kable(data.frame(f), caption = "Foo")
However crosstab output cant be coerced to data.frame while using crosstab function.
Any ideas?
Thanks in advance!
https://haozhu233.github.io/kableExtra/bookdown/index.html
https://github.com/haozhu233/kableExtra/tree/master/docs/bookdown_example
Check out this sample document (I'm still working on this document recently so you may expect some change). Note that you might need the latest dev version of kableExtra and bookdown for some of the formatting features mentioned there.

R2HTML or knitr for dynamic report generation?

I want to write an R function which processes some data and then automatically outputs an html report. This report should contain some fixed text, some text changing according to the underlying data and some figures.
What is the best way to go?
R2HTML or knitr?
What are the advantages of one over the other?
As far as I understood R2HTML allows me to build the html file sequentially while knitr already operates on an predefined .Rhtml file.
So, either use R2HTML or stitch and spin from knitr for on the fly report generation.
I would appreciate any suggestions or hints.
I grab this nice opportunity to promote pander a bit :)
This package was written for similar reasons like #Yihui's great knitr, although I wanted to let users really concentrate on the text and R code without dealing with chunk options etc. So letting users generate pretty HTML, pdf or even docx or odt output automatically with some predefined options.
These options affects e.g. the cache engine (handling dependencies without any chunk options) or the default plot options (let it be a "base" R graphics, lattice or ggplot2), so that you do no thave to set the color palette or the minor grid in each of your plots, just once - or live with the package defaults :)
The package captures the results (besides errors/warnings and other messages and the output) of all run R expression and can convert to Pandoc's markdown automatically. There are some helper functions that let you convert the resulting document written in a brew-like syntax automatically to e.g. HTML if you have pandoc installed, or export R objects to markdown/HTML/any other supported format in a live R session with a reference class.
Short demo:
brew file
Pandoc.brew('file_name.brew', output = 'foo.html', convert = 'html')
HTML output
knitr, every time. Handles graphics, lets you write your report with markdown instead of having to write html everywhere (if you want), caches things, makes coffee for you etc.
You can also build an HTML file sequentially as long as you have a decent text editor like Emacs/ESS or RStudio, etc. R2HTML is excellent in terms of its wide support to many R objects (see methods(HTML)), but I'll probably frown on RweaveHTML() due to its root Sweave().
That said, I think it may be a good idea to combine R2HTML and knitr, e.g.
# A LOESS Example
```{r loess-demo, results='asis'}
cars.lo <- loess(dist ~ speed, cars)
library(R2HTML)
HTML(cars.lo, file = '')
```
I was using the R Markdown syntax in the above example. The key is results='asis' which means to writing raw HTML code into the output.
I believe that you can also use Sweave to create HTML files, though I have heard that knitr is easier to use.

Redirecting R output and graphs

I use Sweave and LaTex to create reports from R output and graphs. But sometime it is required to have graphs in editable format. I tried R2wd package but it doesn't seem very flexible with ggplot2. I'd highly appreciate if someone point out me some efficient ways. Thanks
It really depends what you mean by "editable", and what kinds of files/endpoints you're talking about. There are a lot of discussions out there (e.g. this R-help thread from 2006) about (1) the best options for generating figures to embed in Word (or PowerPoint, which is pretty much the same question) and (2) the best options for figures that can be edited (by which I mean that they can be modified by non-R-users, not just moved from file to file). The general conclusions I have seen are:
PDF files: vector format, editable in Adobe Illustrator ($$$), only sorta-kinda-embeddable in MS Office documents
Windows metafiles or extended metafiles (WMF/EMF): vector format, very limited support outside of the Windows platform. Somewhat wonky format, but MS Office-native. Will certainly have limited support for things like alpha channels (transparency).
SVG: vector format. Very modern, editable in Inkscape (don't know where else), not particularly MS Office-compatible (I think). (Generatable at least via the Cairo package.)
PNG: raster format, but very compact (you can make the resolution absurdly large and still have a reasonably small output file); probably the easiest/lowest-common-denominator solution if you only need portability and not editability.
As of R 2.13.0, Sweave can automatically generate both PDF and PNG files on the fly for each figure chunk. If this is saved as document foo.Rnw:
\documentclass{article}
\begin{document}
\SweaveOpts{png=TRUE,pdf=TRUE,eps=FALSE}
<<fig1,fig=TRUE>>=
plot(1:5,1:5,col=1:5,pch=16)
#
\end{document}
... then after Sweaveing your directory will contain the files foo-fig1.png and foo-fig1.pdf. I don't know if that answers your question, but your question isn't entirely clear ...
The package pgfSweave links together Sweave with tikzDevice. tikzDevice uses the LaTeX tikz package to put the instructions to draw a plot into LaTeX code. So you can copy the resulting code from one file to another. pgfSweave also adds some handy features like cacheing.
You could also just output PDFs of your plots with pdf() and then insert the LaTeX code to load those plots as figures. You lose the automated file management of Sweave that way though.

Resources