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:
Related
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)
```
My code is producing a lot of NULLs at the end in the html output.
Could you please help me to prevent it?
---
title: "Test"
output: html_document
---
```{r warning=FALSE, message=FALSE, results = 'asis',echo=FALSE}
library(tidyverse)
library(knitr)
library(kableExtra)
# nest all data except the cut column and create html tables
diamonds_tab <- diamonds %>%
nest(-cut) %>%
mutate(tab = map2(cut,data,function(cut,data){
writeLines(landscape(kable_styling(kable(as.data.frame(head(data)),
caption =cut,
format = "html",align = "c",row.names = FALSE),
latex_options = c("striped"), full_width = T)))
}))
# print tab column, which contains the html tables
invisible(walk(diamonds_tab$tab, print))
```
Instead of using invisible, wrap the print command with capture.output.
---
title: "Test"
output: html_document
---
```{r warning=FALSE, message=FALSE, results = 'asis',echo=FALSE}
library(tidyverse)
library(knitr)
library(kableExtra)
# nest all data except the cut column and create html tables
diamonds_tab <- diamonds %>%
nest(-cut) %>%
mutate(tab = map2(cut,data,function(cut,data){
writeLines(landscape(kable_styling(kable(as.data.frame(head(data)),
caption =cut,
format = "html",align = "c",row.names = FALSE),
latex_options = c("striped"), full_width = T)))
}))
# print tab column, which contains the html tables
walk(diamonds_tab$tab, ~ capture.output(print(.x)))
```
I found out, that it is only enough to remove the walk.
I am using functions described here to render multiple independent R scripts in a single markdown files. But I am not able to include a formatted equation in those scripts. My .Rmd file looks like:
---
title: "My title"
author: "Nacho"
output:
bookdown::html_document2:
base_format: rmdformats::downcute
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
join <- function(ls, sep = ", "){
do.call(paste, append(ls, list(sep = sep)))
}
inline_render <- function(script_name){
suppressMessages(capture.output(rmarkdown::render(paste0(script_name, ".R"),
output_format = "rmarkdown::md_document"), file = "tmp"))
cat(join(readLines(paste0(script_name, ".md")), "\n"))
}
```
Here is some text defining the scope of the analysis...
```{r script, echo=FALSE, results='asis'}
inline_render("script")
```
And in my script I have defined an equation:
#'
#' $$\frac{d_{A}}{dt} = K_{rel}\times A$$
#'
But when this .R is rendered to .md, I get this warning:
[WARNING] Could not convert TeX math \frac{A}{dt} = K_{rel}\times A, rendering as TeX
and this is what is what I find in the .md
$$\\frac{d\_{DXD}}{dt} = K\_{rel}\\times A\_{T-DXd}$$
So, when this is rendered to the final .html I just the plain text ignoring all \*.
I tried to edit the intermediate .md file to remove one of the \ but the one remaining seems to be ignored by the cat() call.
Could anyone help me with this?
I am using the R programming language. I am trying to combine a HTML file and a JPG Image file together.
My code looks something like this:
library(plotly)
library(shiny)
library(magick)
#create widget_1
widget_1 = plot_ly(iris, x = ~Sepal.Length, type = "histogram", nbinsx = 20)
#upload some jpg image from your computer into R
my_image = image_read("my_image.jpg")
doc <- htmltools::tagList(
div(widget_1, style = "float:left;width:50%;"),
div(my_image,style = "float:left;width:50%;")
)
htmltools::save_html(html = doc, file = "C://Users//Me//Desktop//combined_file.html")
However, this code produces the following error:
Error in as.vector(x, "character"): cannot coerce type 'externalptr' to vector of type 'character'
Is it possible to save html and jpg files together in R? Or is this simply not possible?
Has anyone ever tried do this before?
Thanks
Maybe you can put this in R Markdown and knit it as HTML to get the output in one HTML file.
---
title: "temp"
output: html_document
---
```{r, echo=FALSE, warning=FALSE, message=FALSE}
library(plotly)
library(shiny)
library(magick)
widget_1 = plot_ly(iris, x = ~Sepal.Length, type = "histogram", nbinsx = 20)
#upload some jpg image from your computer into R
my_image = image_read("try.png")
```
```{r, echo=FALSE, warning=FALSE, message=FALSE, fig.height=3}
widget_1
```
```{r, echo=FALSE, warning=FALSE, message=FALSE, out.width = "600px"}
my_image
```
This generates HTML file as :
Question about knitting RMarkdown.
I am having issues when knitting a Rmarkdown file to HTML/pdf. When I run my chunks of code in the Rmarkdown file everything runs smoothly (and I get my graphs made with ggplot) but when knitting I get an output with no graphs and errors (error in eval, error in ggplot, error in print).
Does anyone have experience with this?
The errors:
Error in eval(lhs, parent): object ‘iso3166’ not found
Error in ggplot(inci_100k, aes(long, lat, map.id=mapname,fill=inci)): object ‘inci_100k’ not found
Error in print(INCIPLOT): object ‘INCIPLOT’ not found
The code:
---
title: "R Markdown MAP"
author: "Alexandra V"
date: "1/4/2020"
output:
html_document: default
pdf_document: default
word_document: default
---
```{r,echo = FALSE, warning = FALSE, message=FALSE, error=TRUE}
knitr::opts_chunk$set(cache=TRUE)
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(error = TRUE)
```
Loading the packages we will need for the following analysis.
```{r echo=FALSE, warning=FALSE}
library(tidyverse)
library(lubridate)
library(ggmap)
library(countrycode)
library(grid)
library(gridExtra)
```
To only keep the data needed to make a worldmap of TB incidences, only the relevant data will be taken from the TB_burden_countries_2020-01-04.csv file. Column 1: country names, column 3: iso3 (country codes), column 6: years, column 8: e_inc_100k (estimated incidence all TB forms per 100.000). To make the file easier to work with the names of the columns will be changed to: country, code, year and inci respectively.
```{r, message=FALSE}
TB.burden <- read.csv("TB_burden_countries_2020-01-04.csv")
TBworldINC.map <- as.data.frame(TB.burden[,c(1,3,6,8)], drop=false)
write.csv(TBworldINC.map, 'TBworldINC.map.csv', row.names = FALSE)
tb.INC <- read_csv("TBworldINC.map.csv") %>%
setNames(c("country", "code", "year", "inci"))
```
```{r}
world <- map_data("world")
tb_some_years <- tb.INC %>%
filter(year %in% c(2005, 2010, 2015, 2018))
inci_100k <- tb_some_years %>%
inner_join(iso3166 %>% select(a3, mapname), by = c(code = "a3")) %>%
left_join(world, by = c(country = "region"))
INCIPLOT <- ggplot(inci_100k, aes(long, lat, map_id = mapname,
fill = inci)) +
geom_map(map = world) +
scale_fill_gradient(low = "blue", high = "yellow") +
theme_void() +
coord_map(xlim = c(-180, 180)) +
labs(fill = "Incidence per year") +
facet_wrap(~ year, ncol = 2)
print(INCIPLOT)
```
picture of the output I get in Rstudio
I have had similar issues when making maps in R. One work around is to create your graphs and to save it locally and including the images. The syntax for adding images in R Markdown is ![alt text](path to image)