Displaying regression summary table with stargazer() - html

I wanted to add a table of the regression summary to my html final document with the stargazer() function however I do not understand why it keeps displaying a text format table . Can someone help me please ?
stargazer::stargazer(reg1 , title = "Arrests per capita VS Video games revenue per capita" , type = "latex",keep.stat = c("n","rsq","adj.rsq","f"))

Sounds like you produce your code in an html format, like markdown. I suggest using gtsummary like that:
---
title: "Regression"
author: "Author"
output: html_document
---
## Model
```{r model}
model <- lm(mpg ~ cyl + disp + wt, data=mtcars)
```
## Summary
```{r summary, warning=FALSE, message=FALSE}
library(gtsummary)
tbl_regression(model)
```

Related

Rmarkdown HTML rendering issue

I am trying to print a list of HTML tables, but for some reason when I knit the document, I get the raw HTML code for output instead of the rendered table. Example:
---
title: "html-render-issue"
output: html_document
---
library(tidyverse)
library(gtsummary)
# this table renders correctly:
tbl_summary(iris)
# but this table does not!!
tables <- list(tbl_summary(iris), tbl_summary(cars))
print(tables)
I don't understand why this is happening, I tried indexing into the list with a for loop
for (i in 1:2) {
print(tables[[i]])
}
but this doesn't seem to work either! Short of doing tables[[1]]; tables[[2]] etc. (which does work), is there a way to iterate over the list and get the output I want?
Consider using results = "asis" in the r chunk and then instead of print use knitr::knit_print
```{r, results = "asis", echo = FALSE}
library(gtsummary)
# this table renders correctly:
tbl_summary(iris)
# but this table does not!!
tables <- list(tbl_summary(iris), tbl_summary(cars))
for (i in 1:2) {
cat(knitr::knit_print(tables[[i]]))
}
```
-output
Try with adding %>% as_gt() within the list!
---
title: "html-render-issue"
output: html_document
---
```{r loop_print, results = 'asis'}
library(tidyverse)
library(gtsummary)
tables <- list(tbl_summary(iris), tbl_summary(cars) %>% as_gt())
walk(tables, print) # walk from purrr package to avoid [[1]] [[2]]
```

Looking for a simple way to include profvis output in a Markdown file in R

The title pretty much says it all. I want to profile a function in R (with the profvis package) and display the output in an R Markdown file, without manually creating a screenshot and ideally with no additional packages except the ones that are loaded with profvis, e.g. htmlWidgets and htmltools.
To be more specific I imagine something like:
library(profvis)
profvis_output <- profvis(rnorm(1e06))
htmlwidgets::saveWidget(profvis_output, "provis_output.html")
and then include the thereby created html file in Markdown.
I already tried
htmltools::includeHTML("profvis_output.html")
in the Markdown file but that did not work.
NOTE: I also want it to be renderable to a PDF
This seems to work:
---
title: "Untitled"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(profvis)
```
## profvis
This is an R Markdown document using profvis.
```{r}
profvis({
data(diamonds, package = "ggplot2")
plot(price ~ carat, data = diamonds)
m <- lm(price ~ carat, data = diamonds)
abline(m, col = "red")
})
```

Export mermaid chart from DiagrammeR

I'm attempting to export a Gantt chart from mermaid to a file through R. I'd be happy with any file format, but SVG or PNG would be preferable. I'm trying to automate this, so simply pressing export through the GUI is not an option.
Here's my code:
library(DiagrammeR)
graph <- mermaid("
gantt
dateFormat HH:mm:ss.SSS
title Sample Test Gantt
section A
thing1 : 15:58:51.556, 16:05:23.494
section B
thing2 : 16:02:00.391, 16:20:46.533
section C
thing3 : 16:18:57.352, 16:23:10.700
thing4 : 16:24:11.705, 16:30:30.432
")
graph
And the graph it generates:
This is a reported issues with the DiagrammeR package, so you may want to keep an eye on this page for future updates: https://github.com/rich-iannone/DiagrammeR/issues/66
There are two ways this could be done as a workaround:
Using Webshot
An alternative way of saving the file is to use the webshot package. This uses the external dependency phantomjs to convert the HTML widget to an image. It can be setup as follows:
install.packages("webshot")
webshot::install_phantomjs()
Using your above example:
library(DiagrammeR)
library(magrittr)
gannt %>%
htmltools::html_print() %>%
webshot::webshot(file = "gannt.pdf")
This will save the plot as a PDF, but you can create images by changing the filename i.e. gannt.png.
Using the plotly package
The plotly package has a useful function for exporting HTML widgets:
plotly::export(gannt, file = "mermaid.png")
From what I know about mermaid it is not possible yet to export to svg or other formats. But it is possible to dump many mermaid objects to an HTML via Rmd:
---
title: "Untitled"
author: "Me"
date: "August 1, 2018"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
This is an R Markdown document.
```{r echo=FALSE, warning=FALSE, message=FALSE}
library(DiagrammeR)
graph <- mermaid("
gantt
dateFormat HH:mm:ss.SSS
title Sample Test Gantt
section A
thing1 : 15:58:51.556, 16:05:23.494
section B
thing2 : 16:02:00.391, 16:20:46.533
section C
thing3 : 16:18:57.352, 16:23:10.700
thing4 : 16:24:11.705, 16:30:30.432
")
graph
graph
graph
```
It produces an HTML file with all the graphs in it. Not an optimal solution, but better than trying to produce lots of charts manually.
Marked mysteRious' answer as correct, as it really helped. For anyone in the future that runs into the same issue, here's the full solution I used:
Export.Rmd
---
title: "TestingExport"
author: "me"
date: "August 2, 2018"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r echo=FALSE, warning=FALSE, message=FALSE}
library(DiagrammeR)
graph <- mermaid("
gantt
dateFormat HH:mm:ss.SSS
title Sample Test Gantt
section A
thing1 : 15:58:51.556, 16:05:23.494
section B
thing2 : 16:02:00.391, 16:20:46.533
section C
thing3 : 16:18:57.352, 16:23:10.700
thing4 : 16:24:11.705, 16:30:30.432
")
graph
```
Then use the following command to turn this into HTML:
Rscript -e "rmarkdown::render('...\\Export.Rmd')"

stargazer arima html dont't work

I am trying to use the stargazer package to create a beautiful output in r. But I run into the problem that it works for type = "latex" , but not in type = "html", throwing error Error in column.matrix [r, i]: subscript out of bounds. Any idea how to fix it.
---
title: "Untitled"
author: " "
date: "Sys.Date()"
output: html_document
---
```{r}
data("AirPassengers")
library(forecast)
fit <- auto.arima(AirPassengers)
```
```{r, results='asis'}
library(stargazer)
stargazer(fit)
fit2 <- arima(AirPassengers, order = c(2,1,1), seasonal = c(0,1,0))
stargazer(fit2, type = "latex")
stargazer(fit2, type = "html")
```
Error in column.matrix[r, i] : subscript out of bounds
I think it might be a problem in your first chunk of code carrying over to the second, perhaps introduced by library(forecast) which may be overwriting something that stargazer needs for the table. Also, I know that auto.arima is not supported in stargazer, only the stats::arima. My RMarkdown chunk was:
```{r, results='asis', warning=FALSE, message=FALSE}
library(stargazer)
fit2 <- stats::arima(AirPassengers, order = c(2,1,1), seasonal = c(0,1,0))
stargazer(fit2, type = "html")
```
And it produced this HTML:

knitr Markdown LateX like table within HTML document

I would like to produce "LaTeX-like" table within an HTM document using knitr markdown (.Rmd) through:
knitr::knit2html(input="D:/...Rmd", output="D:/...report.html")
Here is an example. However, if I decided to produce a report, the LaTeX table would be incorrect:
library(xtable)
xtabl <- xtable(head(CO2))
print(xtabl, type="latex", include.rownames=FALSE)
The above gives:
As suggested here is the result. It was NOT a "LaTeX-like" table!
xtabl <- xtable(head(CO2))
print.xtable(xtabl, type="html", include.rownames=FALSE)
EDIT:
What I mean by "LaTeX-like" table is this:
The R Markdown cheat sheet provides a visual comparison of libraries kable, xtable and stargazer. Stargazer could be what you are looking for.
Also have a look into the htmlTable package.
Further customizations could be made with a custom CSS file.
Here's an example of a basic table with htmlTable:
---
title: "Untitled"
author: "Author"
date: "2/5/2017"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
```{r}
library(htmlTable)
```
```{r, results="asis"}
tab = cbind.data.frame(
sapply(iris[1:5 , sapply(iris, is.numeric)], function(x) sprintf("%1.1f", x)),
Species=iris$Species[1:5]
)
htmlTable(tab, rnames=FALSE, align="rrrrr", align.header="rrrrr",
css.cell = c(rep("padding-left: 5em", 4), "padding-left: 2em"))
```
I have used knitr::kable for producing the desired tables.
mydata <- data.frame(SrNo=c(1,2,3,4), websites=c("stackoverflow", "twitter", "facebook", "google"))
knitr::kable(mydata)
kable function accepts a format argument with possible values latex, html, etc. see the documentation for details
Complete Markdown file
---
title: "kable"
author: "Imran Ali"
date: "February 6, 2017"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r, echo=FALSE}
mydata <- data.frame(SrNo=c(1,2,3,4), websites=c("stackoverflow", "twitter", "facebook", "google"))
knitr::kable(mydata)
```