Search code examples
rdataframepdfkable

save_kable as pdf changes structure


I created a table using

df %>%  
    mutate_if(is.numeric,round,digits = 2) %>%
    kable(format="simple", caption="Statistical evaluation")  %>%
    save_kable(file="test.pdf")

Do you know how to fix this? Is there an alternative?

EDIT: This is the data

structure(list(TPR = c(0.934842767295597, 0.92922841571852), 
    FPR = c(0.516589250165893, 0.525067529460655), ACC = c(0.867615330138182, 
    0.866211826128489), FAR = c(0.0881670095698295, 0.0834131044726853
    ), CSI = c(0.85734568558549, 0.856776377557092), BS = c(1.02523463957426, 
    1.01379194951716), HSS = c(0.444709565304963, 0.419220178831526
    ), tr = c(0.22, 0.25)), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -2L))

and this the pdf enter image description here


Solution

  • This can be done entirely in kableExtra. For pdf output typically you do need format = "latex". If you want to edit the appearance of the table kableExtra has plenty of options.

    df1 <- structure(list(TPR = c(0.934842767295597, 0.92922841571852), 
                          FPR = c(0.516589250165893, 0.525067529460655), ACC = c(0.867615330138182, 
                                                                                 0.866211826128489), FAR = c(0.0881670095698295, 0.0834131044726853
                                                                                 ), CSI = c(0.85734568558549, 0.856776377557092), BS = c(1.02523463957426, 
                                                                                                                                         1.01379194951716), HSS = c(0.444709565304963, 0.419220178831526
                                                                                                                                         ), tr = c(0.22, 0.25)), class = c("tbl_df", "tbl", "data.frame"
                                                                                                                                         ), row.names = c(NA, -2L))
    
    
    library(kableExtra)
    
    df1 |> 
      kable(format = "latex",
            digits = 2,
            caption="Statistical evaluation")  |> 
      save_kable(file="test.pdf")
    

    Created on 2023-06-29 with reprex v2.0.2

    enter image description here