kableExtra icon indicating copy to clipboard operation
kableExtra copied to clipboard

Problems generating a 3d Table.

Open ralmond opened this issue 1 year ago • 2 comments

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

ralmond avatar May 31 '24 20:05 ralmond

~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

haozhu233 avatar May 31 '24 22:05 haozhu233

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.

ralmond avatar May 31 '24 22:05 ralmond