papaja
papaja copied to clipboard
Multiple hierachies for col_spanners in apa_table()
Hey Frederik, regardless of the trick I tried, I couldn't get apa_table() to print a second hierachy of column spanners. Are multiple column spanners for groups of groups somehow supported?
e.g. by adding another col_spanner argument, handing over nested lists or adding a third value for level of the col_spanner (as in the example below)?
```{r , results= 'asis'}
library(papaja)
my_table <- t(apply(cars, 2, function(x) # Create data
round(c(Mean = mean(x), SD = sd(x), Min = min(x), Max = max(x)), 2)
))
apa_table(
cbind(my_table, my_table, my_table),
align = c("l", rep("r",12)),
caption = "A summary table of the vehicles dataset.",
note = "This table was created using apa\\_table()",
added_stub_head = "Variables",
col_spanners = list( `My blue Cars` = c(2, 9, 1), `My red Cars` = c(10, 12, 1)), `Cars 1` = c(2, 5, 2), `Cars 2` = c(6, 9, 2), `Cars 3` = c(10,13, 2))) # Is something similar possible?
```
(There is also an open question on stackoverflow)
Hi Jonas,
thanks for bringing that SO question to my attention. Multiple levels of column spanners are currently not supported but it would be a sensible addition. I'm not sure about the best way to do this.
I don't particularly like the idea of multiple column spanner arguments because it doesn't scale. The other two options you suggests are viable; the nested list was the first approach that cam to my mind. Generally, I also like the approach of subsequent function calls to customize tables taken in kableExtra
and stargazer
. That would, however, require some rewriting of the functions and doesn't follow from the current workflow in the package. For comparison:
apa_table(
cbind(my_table, my_table, my_table)
, col_spanners = list(
# Level 1 spanners
"My blue Cars" = c(2, 9, 1)
, "My red Cars" = c(10, 12, 1)
# Level 2 spanners
, "Cars 1" = c(2, 5, 2)
, "Cars 2" = c(6, 9, 2)
, "Cars 3" = c(10, 13, 2)
)
)
apa_table(
cbind(my_table, my_table, my_table)
, col_spanners = list(
list( # Level 1 spanners
"My blue Cars" = c(2, 9)
, "My red Cars" = c(10, 12)
)
, list( # Level 2 spanners
"Cars 1" = c(2, 5)
, "Cars 2" = c(6, 9)
, "Cars 3" = c(10, 13)
)
)
)
apa_table(
cbind(my_table, my_table, my_table)
, col_spanners = list(
# Level 1 spanners
"My blue Cars" = c(2, 9)
, "My red Cars" = c(10, 12)
)
) %>%
# Level 2 spanners
add_col_spanner(
"Cars 1" = c(2, 5)
, "Cars 2" = c(6, 9)
, "Cars 3" = c(10, 13)
)
Do you have any thoughts on this?
Hm, like you say, the first two approaches seem to be most consistent with the current workflow and are, from my perspective, a bit more transparent.
The first approach is also quite flexible in regards of the order of the lists, as its specifics the location of the column spanner regardless of positon or nested-ness in list. To me it seems to be more readable as well, espeically in complex situations (but I guess you shouldn't be to exzessive entering column spanners anyway).
It would be equal to:
apa_table(
cbind(my_table, my_table, my_table)
, col_spanners = list(
"My blue Cars" = c(2, 9, 1) # Level 1 spanner
, "Cars 1" = c(2, 5, 2) # Level 2 spanner
, "Cars 2" = c(6, 9, 2) # Level 2 spanner
, "My red Cars" = c(10, 12, 1) # Level 1 spanner
, "Cars 3" = c(10, 13, 2) # Level 2 spanner
)
)
Randomly came across this issue online. If you are willing to export the results as an object, I can let kableExtra take apa_table results too.
This example here capture the results of apa_table and fake it as a kable
. The key part is to return the latex result and give it a class name (could be papaja_table
or similar in this case).
library(papaja)
library(kableExtra)
apa_table.latex.as_kable <- function(...) {
# BTW, I think you probably want to set escape = F by default in https://github.com/crsh/papaja/blob/master/R/apa_table.R#L258
out <- capture.output(apa_table.latex(..., escape = F))
out <- paste(out, collapse = "\n")
class(out) <- "knitr_kable"
attr(out, "format") <- "latex"
return(out)
}
my_table <- t(apply(cars, 2, function(x) # Create data
round(c(Mean = mean(x), SD = sd(x), Min = min(x), Max = max(x)), 2)
))
apa_table.latex.as_kable(my_table) %>%
add_header_above(c(" ", "A" = 2, "B" = 2)) %>%
add_header_above(c(" ", "C" = 3, "D" = 1))
That sounds great! We are in the process of turning the output of the packages functions into S3 classes. I'll look into it as soon as I find some time!
Hello, I am trying to add multiple col_spanners but am running into errors. Is there a solution to this issue? Thank you in advance.
Hello, I am trying to add multiple col_spanners but am running into errors. Is there a solution to this issue? Thank you in advance.
I happened across this today in search of the answer myself. A workaround I used was to format the table directly in LaTex and include a LaTex chunk in the markdown script. If bookended with the same Tex script as apa_table outputs, it is formatted the same.
\begin{table}[tbp]
\begin{center}
\begin{threeparttable}
\caption{\label{tab:transformtable}}
\begin{tabular}{llllll}
Enter your LaTex table code and contents here, using \multicolumn{}{}{} and bookend with:
\end{tabular}
\end{threeparttable}
\end{center}
\end{table}
My final complete code was
\begin{table}[tbp]
\begin{center}
\begin{threeparttable}
\caption{\label{tab:transformtable}}
\begin{tabular}{llllll}
\toprule
& &\multicolumn{2}{c}{Shapiro-Wilks} &\multicolumn{2}{c}{\emph{p}-value} \\
\cmidrule{3-6}
Measure & Transform Power & \multicolumn{1}{c}{Original} & \multicolumn{1}{c}{Updated} & \multicolumn{1}{c}{Original} & \multicolumn{1}{c}{Updated}\\
\midrule
OVT & 1.425 & .96 & .98 & .006 & .036\\
PURL & .025 & .70 & .99 & <.001 & .008\\
\bottomrule
\end{tabular}
\end{threeparttable}
\end{center}
\end{table}