Home > Net >  Transform datatable to a dataframe in R
Transform datatable to a dataframe in R

Time:10-20

I would like to transform z to a dataframe for using write_xlsx . I try z<-data.frame(z), but it didn't work. Any solution for this?

library(DT)
library(writexl)

Test <- structure(list(date2 = structure(c(18808, 18808, 18809, 18810
), class = "Date"), Category = c("FDE", "ABC", "FDE", "ABC"), 
coef = c(4, 1, 6, 1)), row.names = c(NA, 4L), class = "data.frame")


z<-datatable (Test, options = list(columnDefs = list(list(className = 'dt-center', targets = "_all")),
                                   paging =TRUE,searching = FALSE, pageLength =  10,dom = 'tip',scrollx=T),rownames = FALSE)


write_xlsx(z, 'C:/Users/Carlos/Desktop/datas.xlsx')

CodePudding user response:

The data is under a couple layers so you have to fish it out. In this case it's in the 'x' element and under a table called 'data'. Often complicated structures like this (class(z) returns "datatables" "htmlwidget") will have the data nested like this, so it just takes a bit of digging.

as.data.frame(z$x$data)

Best, Mostafa

  • Related