I´ve got a data frame that I would like to make accessible via R plumber.
I am using knitr do to this.
#* html table
#* @serializer html
#* @get /test
function() {
data <- iris
knitr::kable(data, "html")
}
Now I would like to add filter/sorting options and maybe color every 2nd row light gray. I though about using datatable from the DT package. I use this in shiny a lot. My attempts at using the package failed.
- DT::renderDataTable(dt) leads to "...meant to be used when embedding snippets of Shiny code..."
- Just trying datatable(iris) leads to "Not compatible with requested type: [type=list; target=raw]"
With ChatGPT I wrote some simple datatable html which works, but I am guessing there is a way to use an existing package to do this in a much simpler way..? Thank you!
my_DT <- function(table){
html_response <- paste0(
"<!DOCTYPE html><html><head>",
"<link rel=\"stylesheet\" ",
"href=\"https://cdn.datatables.net/1.10.20/css/jquery.dataTables.min.css\">",
"<script src=\"https://code.jquery.com/jquery-3.5.1.min.js\"></script>",
"<script src=\"https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js\"></script>",
"</head><body>",
table,
"<script>$(document).ready(function(){$('table').DataTable();});</script>",
"</body></html>"
)
html_response
}
#* data table test
#* @serializer html
#* @get /test
function() {
data <- iris
# dt <- DT::datatable(data, options = list(pageLength = 5, autoWidth = TRUE))
my_DT(data)
}