Amit Peer
Amit Peer

Reputation: 17

How to display numerically number of displayed results based on specific input in R Shiny?

library(shiny)

ui <- fluidPage(

titlePanel("BOILER MASTER DATA LIST"),
sidebarLayout(

sidebarPanel(
selectizeInput("Status", "Select a Status", choices = NULL)),

mainPanel(
tableOutput("BOILERDATA")),))

server <- function(input, output, session){(

updateSelectizeInput(session,"Status", choices = RawData$Status, server = TRUE)

output$BOILERDATA <- renderTable({statefilter <- subset(RawData, RawData$Status == input$Status, drop = TRUE)})}

shinyApp(ui = ui, server = server)

I am trying to add output in server part showing no. of results displayed based on input selected. Please help.

Upvotes: 0

Views: 138

Answers (1)

MrGumble
MrGumble

Reputation: 5766

You need an extra output, e.g. a textOutput, in you UI. renderTable only handles tables and table-like structures. If you wanted something written beneath the table, you would literally have to append a row with the text. Ever done that in Excel? Then you know you'll also have to handle textoverflow, spanning cells, perhaps borders, etc. And it kinda breaks the idea that you have your tabular data and random non-tabular data, in a tabular scaffold.

Then you need to assign it some output. Ideally nrow(statefilter), but you can't because it is not defined outside of the scope of your renderTable. Instead of preparing your data in the table, and then somehow assigning it to a variable that other scopes can find, we break the task up in two distinct parts: 1) preparing the (subsetted) data, and 2) formatting it for presentation. Currently, you are doing both in renderTable. Which by itself is okay, it just makes it harder to build further on. So, to do part 1 outside of renderTable you have to do it in a reactive context, because it uses a reactive variable as input (input$Status).

filtered <- reactive({
  req(length(input$Status) == 0)
  subset(RawData, RawData$Status == input$Status, drop = TRUE) 
})
output$BOILERDATA <- renderTable( filtered() )
output$rows <- renderText( nrow(filtered() )

Do yourself a favour and lookup what req does.

Upvotes: 1

Related Questions