I have a shiny app with a datatable, and I would like to have 2 things done:
- I'd like to add the filters at the top of the DTOutput
- I'd like to have the numbers in the table formatted as percentages.
I can only achieve one or the other. So if I only use the filters (see attempt #3 in the code) it works, but I cannot format the numbers as percentages. If I try to format the numbers (see attpemts #1 and #2 in the code) then the filters disappear but the numbers are correctly formatted. I also get warning message in the console:
"renderDataTable ignores ... arguments when expr yields a datatable object"
which doesn't help me much, since I am quite new to Shiny and R.
I found tutorials and answered questions for either formatting numbers or filtering tables, but I am obviously missing something... If you can guide me to an answer or spot a mistake in the code below I would be thankful.
reproducible app.R
here:
library(shiny)
library(dplyr)
library(DT)
# Define UI
ui <- fluidPage(
actionButton("start", "Click to Start")
DTOutput('tbl1'),
DTOutput('tbl2'),
DTOutput('tbl3')
)
# Define Server
server = function(input, output) {
#Attempt #1: gives me the formatted numbers but no filter.
x <- eventReactive(input$start, iris %>% dplyr::filter(Species == "setosa") %>% datatable %>% formatPercentage(2:3, digits=2))
output$tbl1<- DT::renderDT(x(), filter="top")
#Attempt #2: gives me the formatted numbers but no filter.
y <- eventReactive(input$start, iris %>% dplyr::filter(Species == "setosa"))
output$tbl2 <- DT::renderDT(y() %>% datatable %>% formatPercentage(2:3, digits=2), filter="top")
#Attempt #3: I get the filter, if I don't try to format the numbers
z <- eventReactive(input$start, iris %>% dplyr::filter(Species == "setosa"))
output$tbl3 <- DT::renderDT(z(), filter="top")
}
# Run the application
shinyApp(ui = ui, server = server)