I am trying to display a grouped data frame output in an interactive DT::datatable with Download buttons (csv,excel) within my Rmarkdown report (HTML).
Everything works smoothly until I encounter an error stating that there is no group by method applicable for object classes datatable and htmlwidgets.
Your assistance would be greatly appreciated.
Below is the code snippet:
## Grouping columns
Site <- LETTERS[1:6]
Block <- LETTERS[1:4] ## For each Site
Plot <- paste(rep("P",10),seq(1,10,1),sep="_") ## For each Block
df <- expand.grid(Site = Site, Block = Block, Plot = Plot)
## Dependant variables
df <-cbind.data.frame(df,data.frame(Tmin=runif(min=-3,max=18,n=240),
Tmax=runif(min=10,max=39, n=240),
Index1=runif(0,5,n=240),
Index2=runif(1,10,n=240)))
# Export grouped df as datatable
library(dplyr)
df%>%
group_by(Site,Block,Plot)%>%
summarize(Tmin_avg=mean(Tmin), Tmax_avg=mean(Tmax))%>%
DT::datatable(
extensions = 'Buttons', options = list(
dom = 'Bfrtip',
buttons =
list('copy', 'print', list(
extend = 'collection',
buttons = c('csv', 'excel', 'pdf'),
text = 'Download'
))
))