'Saving datatable to pdf in R

I've created a datatable with R and I try to save it.
However, I can't do that due to the error:

Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) : 
 cannot coerce class ‘c("datatables", "htmlwidget")’ to a data.frame

My the most accurate attempt:

pdf(file = "file.pdf")
df <- pData(bladderEset)
df <- df %>% rownames_to_column("id")
df <- df[, c('id', 'batch', 'cancer')]
df <- datatable(df) %>% formatStyle(
    c("cancer", "id"),
    valueColumns = "cancer",
    color = styleEqual(c('Normal', 'Cancer', 'Biopsy'), c('black', 'red', 'blue'))
)
grid.table(df)
dev.off
r


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source