gt table in R Markdown not being outputted - r

I just discovered the gt library and was excited to try it out but no matter what I did, the tables keep being outputted as a function???
The code is the same on the homepage of the gt website, the gt(x) is literally being processed as a function as we can see from the picture. I updated all my packages and even installed the developer version as well but I keep getting this function output
gt table function output, no table

You can use the gtsave function and save as a .png and it will output. For example, here is a plot that I generated with the following code:
{r mOS Table Generation, fig.dim=c(6,8)}
mostable <- mosdf %>%
ungroup %>%
select(-Dose) %>%
gt() %>%
tab_header(title = "Median Overall Survival (mOS)", subtitle = md("*Subjects Treated from REDACTED*")) %>%
tab_spanner(
label = "95% Confidence Interval",
columns = c(5:6)) %>%
cols_align(align = "center") %>%
tab_style(cell_text(weight = "bold"),
locations = list(cells_title(groups = c("title", "subtitle")),
cells_column_spanners(spanners = everything()),
cells_column_labels(columns = c(1:4)))) %>%
tab_style(cell_text(style = "italic"),
locations = cells_row_groups(groups = everything()))
setwd(output)
gtsave(mostable, "mostable.png", vwidth = 1500, vheight = 1000)
Here is how it looks the R Markdown when knitted to Word:
Note, even though I have the dimension statement in the chunk code, gtsave is dependent on webshot, so the vwidth and vheight control the size of the output to the working directory. Most of the time the output size is fine, but I am still working through how to change the output size. Most of the time I just manually do it, but prefer to not have to update the output. I am going to ask another question, so I will link if there is a solution.

My apologies, turns out there's also a function called "gt" in the expss package I had loaded that interfered with it, looks like I'm not getting my hour back from trying to troubleshoot this lol

Related

R great tables package - how to draw custom horizontal lines

I would like to be able to draw custom horizontal lines at certain rows using R's gt package. I've worked out the remove the default horizontal lines but the code I'm trying (see the last two lines below), which gives no errors does not seem to draw any lines. Help appreciated.
require(gt)
gt(iris) %>%
tab_spanner(label = "Sepal", columns = c(Sepal.Length, Sepal.Width)) %>%
tab_spanner(label = "Petal", columns = c(Petal.Length, Petal.Width)) %>%
cols_label(Sepal.Length = "Length", Sepal.Width = "Width") %>%
tab_options(table_body.hlines.color = "transparent") %>%
tab_style(style = cell_borders(sides = c("bottom"), weight = px(0.5)),
locations = cells_body(rows = c(3, 4, 7)))
I would also be interested in how to draw a horizontal line across say only he first three columns for a given row but will post that as a separate question, should that be out of scope for one question.
In fact, your code draws horizontal lines on the requested rows (3, 4, and 7). I just ran it and it works. Is this result what you expect?
Tested with R 4.2.1, gt package version 0.8.0 using Rstudio 2022.12.0

webshot2::webshot is trimming the right side off of a huxtable in R

I am trying to convert an html table created in R via the huxtable package to a png file using webshot2::webshot. Unfortunately, the very right side of the output seems to get trimmed off and I can't figure out how to fix this generically.
I don't want to manually tweak the cliprect parameter because I'll need to do this for many tables and its not scalable if its not generic. However, it is possible to achieve it with this parameter so I wonder why its failing with the other workflow.
Here's an example illustrating the problem:
library(magrittr)
library(huxtable)
set.seed(1337)
data <- matrix(rnorm(25), 35, 10)
my_hux <- as_hux(data) %>%
set_outer_borders(0.4) %>%
map_background_color(by_rows("grey95", "white")) %>%
map_text_color(by_quantiles(c(0.1, 0.9), c("red", "black", "green3")))
quick_html(my_hux, file = "ahuxtable.html", open = FALSE)
webshot2::webshot(url = "ahuxtable.html", file = "ahuxtable.png",
zoom = 5, selector = "table")
I tried this with webshot::webshot, however the webshot package seems to be webshot2's predecessor so I'd prefer a webshot2 solution if there is one.

How to create tables in R

I am trying to create and export a table for publication (picture attached).
I have created a table using the code below, but I could not export it as a table.
Can anyone help, please
library(tidyverse)
library(gapminder)
data(gapminder)
median_gdp <- median(gapminder$gdpPercap)
gapminder %>%
select(-country) %>%
mutate(gdpPercap = ifelse(gdpPercap > median_gdp, "high", "low")) %>%
mutate(gdpPercap = factor(gdpPercap)) %>%
mutate(pop = pop / 1000000) -> gapminder
gapminder <- lapply(gapminder, function(x) x[sample(c(TRUE, NA),
prob = c(0.9, 0.1),
size = length(x),
replace = TRUE
)])
library(arsenal)
table_one <- tableby(continent ~ ., data = gapminder)
summary(table_one, title = "Gapminder Data", text=TRUE)
If you want to write a table to Microsoft Word, you can use the following code from arsenal package.
write2word(table_one, "table.doc",
keep.md = TRUE,
quiet = TRUE,
title = "Your title")
You can also write a table to pdf and HTML by using the arsenal package. For the details, see
?write2specific
Weirdly, there doesn't seem to be a general question on this topic, though see Create a PDF table for PDFs.
Modern packages to print tables in output formats, including PDF, HTML and Word, include gt, huxtable, flextable and kableExtra.
Packages to create tables of summary statistics include skimr, summarytools and qwraps2. Some of these also have built-in output to different formats.
There are many other packages out there.

ggSave group_by df list of ggarrange'd ggplot objects

I've used group_by, do, and ggplot - twice - to create two simple dfs of Date (the group) and a list of the ggplot outputs, thanks hugely to help from examples on this site. Simplified example:
p1 <- df_i %>% group_by(Date) %>% do(
plots = ggplot(data = .) +
geom_line() #etc, hugely long and detailed ggplot call omitted for brevity, but it works fine
) # close do
I can then join those dfs,
p1 <- cbind(p1, p2[,2])
names(p1) <- c("Date", "Temp", "Light") #Temp & Light were both "plots" from above
And loop through the rows, saving the outputs in a 1-row (top & bottom object) ggarranged png:
for (j in 1:nrow(p1)) {
ggsave(file = paste0(p1$Date[j], ".png"),
plot = arrangeGrob(p1$Temp[[j]], p1$Light[[j]]),
device="png",scale=1.75,width=6.32,height=4,units="in",dpi=300,limitsize=TRUE)
}
So far, so good. But nature abhors a for-loop, so I was trying to do the ggsaving in a group_by, using the same ggsave parameter options, changing only what's needed given the difference in for-loop indexing vs (what I understand of) group_by subsetting:
p1 %>% group_by(Date) %>%
ggsave(file = paste0(.$Date, ".png"),
plot = arrangeGrob(Temp, Light),...) #other params hidden here for brevity
Error in grDevices::png(..., res = dpi, units = "in"): invalid
'pointsize' argument
If I add pointsize=10 it says "invalid bg value"; add bg = "white":
Error in check.options(new, name.opt = ".X11.Options", envir =
.X11env) : invalid arguments in 'grDevices::png(..., res = dpi,
units = "in")' (need named args)
(I also tried lowering dpi to no effect). Possibly I'm going about this the wrong way, e.g. swapping %>% for %$% in Vlad's suggestion from magrittr:
Error in gList(list(list(data = list(DateTimeUTCmin5 = c(915213660, 915213780, :
only 'grobs' allowed in "gList"
This gives the same error with Date and .$Date in the ggsave call. Trying to recreate the do framework:
p1 %>% group_by(Date) %>%
do(ggsave(file = paste0(.$Date, ".png"),"_", .$Date, ".png"),
plot = arrangeGrob(Temp, Light), #etc
Error in arrangeGrob(Temp, Light) : object 'Temp' not found
p1 %>% group_by(Date) %>%
do(ggsave(file = paste0(.$Date, ".png"),"_", .$Date, ".png"),
plot = arrangeGrob(.$Temp, .$Light), #etc
Error in gList(list(list(data = list(DateTimeUTCmin5 = c(915213660,
915213780, : only 'grobs' allowed in "gList"
Which gives the same error if I use %$%.
Does anyone have the connected stack of understanding of these tools such that they can see what I'm doing wrong here? It seems like I should be close, but I'm increasingly groping around in the dark. Any pointers very much appreciated. Thanks in advance!
Equally if folks recommend a different approach I'm interested too. It strikes me that I could use an lapply (or parSapply) instead of the for-loop on the p1 df. Do operations on grouped dfs outperform apply operations?
[Edit: desired final output: ggsave dumps 1 image (with 2 plots on it) per Date, into the specified folder. Essentially if I can get ggsave to work within the grouped_df, that should be that]

Why I get different results when running code in R Markdown with knitr than in R script

I have an R code that I've written which is in a normal .r file, now I want to make a markdown html report so I'm basically running the same code, but in chunks with text in between.
I have the weirdest problem where some code works as it worked in the regular r file, but some code produces different results entirely. For example:
mydata_complete_obs %>% select(-(prom_id:end_a)) %>% select(qualified, everything()) %>%
cor(use = "complete.obs", method = "spearman") %>%
corrplot(type = "lower", method = "circle", diag = F, insig = "pch", addCoef.col = "grey",
p.mat = res1$p, title = "Spearman Correlations")
The above code which produces a corrplot does work and produces the same graph as I get in the .r file, but a simple summary() function gives me different things - the correct output being produced in the .r file and in the markdown report I get all zeroes (min, 1st quartile, median, mean, etc. - all 0!). This is the chunk for the summary():
```{r hists, echo = FALSE, warning = FALSE, message = FALSE, error =
FALSE, results="markup"}
summary(mydata_complete_obs)
```
What can be wrong? I'm loading all the libraries and read in the data from an .rds file in the first chunk, and then later use mydata_complete_obs to produce graphs and summaries. If I understand correctly I don't need to load the data for each chunk separately, because I thought that was the problem.
Ok, I have solved the problem. I had to change the query in the database so that the format of the columns will be INT instead of BIGINT (that's in Impala), then when I read it in R, everything worked and the strange behaviour disappeared.

Resources