haozhu233 / kableExtra

Construct Complex Table with knitr::kable() + pipe.
https://haozhu233.github.io/kableExtra/
Other
688 stars 146 forks source link

Problems generating a 3d Table. #847

Open ralmond opened 3 months ago

ralmond commented 3 months ago

Describe the bug

I'm using ftable to render a 3d array, but when I pass the output of ftable to kbl, I get an error about bad arguments to an internal sprintf function.

To Reproduce

kableExtra::kbl(ftable(array(runif(50), c(5, 2, 5))))
#> Error in sprintf("   <td%s> %s </td>", align, z): arguments cannot be recycled to the same length

Created on 2024-05-31 with reprex v2.1.0

haozhu233 commented 3 months ago

~I think kable and functions in this package only addresses 2d array. In my impression, this is probably true for most table generation packages. It probably makes more sense to use some plotting methods to complete this task.~

Sorry, I should have thought about it more carefully before throwing out comments. This feature is possible. Let me see what can be done here

ralmond commented 3 months ago

The purpose of ftable is to flatten the 3d array into a matrix (sort of like going from wide to long format using dplyr.

I think that the problem is that ftable is stored as some kind of ragged array. If manually do the step using rbind, it seems to work:

phi2 <- array(runif(50),c(5,5,2))
kableExtra::kbl(rbind(phi2[,,1],phi2[,,2]),digits=3,booktabs=T) |> 
  kableExtra::kable_classic_2() |>
  kableExtra::pack_rows("Y=0",1,5) |>
  kableExtra::pack_rows("Y=1",6,10)

This produces what I want, I just need to be a bit more manual about unrolling the array.

Also, kbl(ftable(array(runif(50),c(5,5,2))) works, but kbl(ftable(array(runif(50),c(5,2,5))) does not.

Finally, I'm realizing the problem is actually upstream in knitr::kable. I should probably refile the bug upstream.