Quantcast
Channel: Active questions tagged kable - Stack Overflow
Viewing all articles
Browse latest Browse all 105

Formatting certain rows in a table with kable and row_spec in rMarkdown not working

$
0
0

I want to create a summary statistics table for my panel data with rMarkdown. I used sumtable()/st() from the package vtable in order to generate summary statistics by group (industries). The output is then handed over to kable for formatting. That is where my problem arises: I want to emphasize the group headings by formatting them bold and italic as well as with a lightgray background (see screenshot).

Unfortunately, this only works for fewer than half of the selected rows. As you can see from the code, I feed a vector of the row numbers that I want to emphasize to row_spec. No matter what I do, I cannot get it to apply the formatting to all the rows selected in the vector. Only rows 109, 121, 133, 145 and 157 are formatted accordingly, while the other rows (1, 13, 25, 37, 49, 61, 73, 85, 97) are left completely unchanged. Interestingly, if I put "1:5" in the first slot of the vector, it works for rows 2 to 5, but not for 1. I don't get it and I am going mad about this...

So, how can I get this to run, so that all the industry group name rows are emphasized?

This is the code I am running. I am sure it is flawed in other ways as well (I am quite new to R).

labs <- c(NA,NA,NA,NA, "H-LD", "H-TS",NA,NA,NA,NA,NA,NA,NA,NA, "Capital Intensity",NA,NA,NA,NA,NA)summ_stat <- st(Data_log, summ = c('mean(x)', 'sd(x)', 'min(x)', 'max(x)'),                                   group = 'Industry', vars = c("Hirings","Layoffs and Discharges", "Total Separations", "Investment in fixed assets","H_LD","H_TS","Fixed assets","Full-time equivalent employees","Full-time and part-time employees","Gross output","Gross capital stock","Value added","Real value added","Gross operating surplus","CL","Capacity utilization","Normal output","Productivity","SLTC","IROR"),                 labels = labs,                group.long = TRUE,                 col.breaks = c(10),                out = 'return') summ_stat$Variable <- sub("Industry: ", "", summ_stat$Variable)kable(summ_stat, caption = "Summary Statistics by Industry", format = "latex", longtable = TRUE) %>%   row_spec(c(1,13,25,37,49,61,73,85,97,109,121,133,145,157), bold = T, color = "black", background = "lightgray", italic = T) %>%  kable_styling(position = "left",              htmltable_class = "lightable-classic",              latex_options= "hold_position") %>%   kable_classic(full_width = F, html_font = "Cambria")

Below are two screenshots of the rendered PDF. As you can see, the first selected rows (only first page of table shown) are not formatted like specified, whereas the last 5 selected rows are.

First rows unformatted

Latter rows are formatted

My data looks like the df below, except that there are way more variables (see vars in code chunk above):

Industries <- c("Mining and logging","Construction", "Durable goods manufacturing","Nondurable goods manufacturing","Wholesale trade","Retail trade","Information","Educational services","Health care and social assistance","Arts, entertainment, and recreation","Accommodation and food services","Professional and business services", "Transportation, warehousing, and utilities","Other services")set.seed(123)df <- data.frame(    Year = 2001:2021,    Industry = rep(Industries, each = 21),    Value1 = sample(300, 294),    Value2 = sample(300, 294))

Viewing all articles
Browse latest Browse all 105

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>