Stargazer in a bookdown site - r

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.

Related

How can I fix knit tables in RMarkdown?

I have an RMarkdown document that has some data wrangling parts within it. I can see all tables calculated as they should look in RStudio once computed. However, once I knit to HTML, I can see all the outputs, however some tables look very messy. Does anyone have any idea how to fix this? Refer to below picture.
Why do the two columns below not appear next to the other ones in one line?
You could try a few things, starting with round down some of the decimals so everything fits.
If you need the decimals you could try printing it with knitr::kable(table). This will help with the tables looking messy.
For even more control there is a library kableExtra that has a function kable_styling:
library(kableExtra)
table %>%
knitr::kable() %>% # you can control column names, row names, rounding of table here
kableExtra::kable_styling(latex_options = c("scale_down") # will fit table
kable_styling has an argument full_width for HTML tables too. For more information on this library see this link

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

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.

How to align table columns i PDF with `texreg`

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.

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.

Resources