Trying to have a table update based on user input in Shiny App

Viewed 34

I am trying to have it so that when a user selects a specimen range and species, the table will only show that range.

The code below creates the table and the inputs, but there appears to be no communication between the two. What am I missing?

Load Libraries

library(shiny)
library(tidyverse)
library(tidytext)
library(glue)
library(plotly)

Read Data

iris <- read_rds("iris.rds")

iris

ui <- fluidPage(
  titlePanel("Flower dimensions by species"),
  sidebarLayout(
   sidebarPanel(
      selectInput(
        inputId = "species",
        label = "Select species",
        choices = c("setosa", "versicolor", "virginica")
      ),
      sliderInput(
        inputId = "specimen_selection",
        label = "Select specimen range",
        min = min(iris$id),
        max = max(iris$id),
        value = 50
      )
    ),
    mainPanel(
      dataTableOutput(outputId = "Specimen_details")
    )
  )
)

server <- function(input, output) {
  output$Specimen_details <- renderDataTable(iris)
}

Shiny App

shinyApp(ui = ui, server = server)
1 Answers

i don't know what iris$id is, but the problem was that renderDataTable() had no inputs to "listen" to, so the table was static.

library(shiny)
library(tidyverse)

data(iris)

ui <- fluidPage(
    titlePanel("Flower dimensions by species"),
    sidebarLayout(
        sidebarPanel(
            selectInput(
                inputId = "species",
                label = "Select species",
                choices = c("setosa", "versicolor", "virginica"),
                multiple = TRUE
            )
        ),
        mainPanel(
            dataTableOutput(outputId = "Specimen_details")
        )
    )
)

server <- function(input, output) {
    output$Specimen_details <- renderDataTable({dplyr::filter(iris, Species %in% input$species)})
}

shinyApp(ui = ui, server = server)
Related