0
votes

Using Rmarkdown and kable, I need to repeat several tables with the same formatting but with different datasets.

For example, in the following two chunks, what is changing is the DF, the variable used to sort and the caption.

Is there any way to avoid repeating 90% of the same code for each chunk?

kable(Crib1 %>% arrange(-Z1) %>%
  select(Ranking = TYP, SID, Statement, FA1:FA3), longtable = TRUE, booktabs = TRUE, caption = "Crib Sheet - Factor 1") %>% 
  collapse_rows(1, latex_hline = "major", valign = "top") %>%
  kable_styling(full_width = FALSE, latex_options = c("hold_position", "condensed", "repeat_header"), font_size = 9) %>%
    column_spec(1, bold=TRUE) %>%
    column_spec(3, width = "18em", italic = TRUE) %>%
    column_spec(4, bold = TRUE)
kable(Crib2 %>% arrange(-Z2) %>%
  select(Ranking = TYP, SID, Statement, FA1:FA3), longtable = TRUE, booktabs = TRUE, caption = "Crib Sheet - Factor 2") %>% 
  collapse_rows(1, latex_hline = "major", valign = "top") %>%
  kable_styling(full_width = FALSE, latex_options = c("hold_position", "condensed", "repeat_header"), font_size = 9) %>%
    column_spec(1, bold=TRUE) %>%
    column_spec(3, width = "18em", italic = TRUE) %>%
    column_spec(4, bold = TRUE)

I am trying to build a function

createKableCrib <- function(factor){
  Crib <- rlang::sym(paste("Crib", factor, sep=""))
  Z <- rlang::sym(paste("Z", factor, sep=""))
  cap <- paste("Crib Sheet - Factor", factor, sep=" ")

kable(!!Crib %>% arrange(!!(-Z)) %>%
        select(Ranking = TYP, SID, Statement, FA1:FA3), 
      longtable = TRUE, booktabs = TRUE, caption = cap) %>% 
  collapse_rows(1, latex_hline = "major", valign = "top") %>%
  kable_styling(full_width = FALSE, latex_options = c("hold_position", "condensed", "repeat_header"), font_size = 9) %>%
  column_spec(1, bold=TRUE) %>%
  column_spec(3, width = "18em", italic = TRUE) %>%
  column_spec(4, bold = TRUE)
}

createKableCrib("1")

But I get the following error:

Error in UseMethod("arrange_") : 
  no applicable method for 'arrange_' applied to an object of class "name" 

Best,

Damien

2
Make it a function?G5W
I am trying to do that. I will edit the post to show the function I am trying to build and the problem it raises..djourd1

2 Answers

1
votes

It looks like you may have gotten there already, but here is an example that seems to work with the use of Curly-Curly instead of the original Bang-Bang (https://www.brodrigues.co/blog/2019-06-20-tidy_eval_saga/).

Didn't have your data, but tried with mtcars and iris. If your columns are the same you could add the select statement after arrange.

library(dplyr)
library(knitr)
library(kableExtra)

create_kable <- function(data, column, title) {
  kable({{data}} %>% 
          arrange({{column}}),
        longtable = TRUE, booktabs = TRUE, caption = title) %>% 
  collapse_rows(1, latex_hline = "major", valign = "top") %>%
  kable_styling(full_width = FALSE, latex_options = c("hold_position", "condensed", "repeat_header"), font_size = 9) %>%
    column_spec(1, bold=TRUE) %>%
    column_spec(3, width = "18em", italic = TRUE) %>%
    column_spec(4, bold = TRUE)
}

create_kable(mtcars, mpg, "Crib Sheet - Factor 1")
create_kable(iris, Sepal.Length, "Crib Sheet - Factor 2")
0
votes

Here is what I found so far. Not completely satisfactory, as I wanted to be able to create the df name within the function, but I keep on getting errors.

createKableCrib <- function(df, factor){
  Z <- paste("Z", factor, sep="")
  cap <- paste("Crib Sheet - Factor", factor, sep=" ")
 
  kable(df  %>%  arrange_at(.vars=desc(Z)) %>%
        select(Ranking = TYP, SID, Statement, FA1:FA3), 
      longtable = TRUE, booktabs = TRUE, caption = cap) %>% 
  collapse_rows(1, latex_hline = "major", valign = "top") %>%
  kable_styling(full_width = FALSE, latex_options = c("hold_position", "condensed", "repeat_header"), font_size = 9) %>%
  column_spec(1, bold=TRUE) %>%
  column_spec(3, width = "18em", italic = TRUE) %>%
  column_spec(4, bold = TRUE)
}

createKableCrib(Crib1, "1")