如何使用下拉输入过滤 Dataframe 在 R Shiny 中呈现数据 Table?

How to render Data Table in R Shiny using Dropdown input to filter Dataframe?

为了继续我之前的 post 应用 map,我正在尝试使用下拉输入在 R Shiny 中过滤 table:

代码:

library(shiny)
library(shinydashboard)
library(tidyverse)
library(leaflet)
library(readxl)
library(RCurl)
library(DT)

URL <- "https://www.mohfw.gov.in/pdf/PMJAYPRIVATEHOSPITALSCONSOLIDATED.xlsx"
download.file(URL, destfile = "../../timesnow_PMJAYPRIVATEHOSPITALSCONSOLIDATED.xlsx",method = "curl")

# Data
ind_vaccination_center <- readxl::read_xlsx(path = "../../timesnow_PMJAYPRIVATEHOSPITALSCONSOLIDATED.xlsx",
                                             sheet = 1)

ind_vaccination_leaflet <- ind_vaccination_center %>% 
    mutate(label_display = paste(
        "<h2>", ind_vaccination_center$`Name of the Vaccination Site*`, "</h2>",
        "<h4>",ind_vaccination_center$`District*`,",", ind_vaccination_center$`State*`, "</h4>",
        "<p>", "Address: ", ind_vaccination_center$Address,",", ind_vaccination_center$`PinCode*`, "</p>",
        "<p>", "Mobile: ", ind_vaccination_center$`Mobile Number`, "</p>",
        "<p>", "Contact Person: ", ind_vaccination_center$`Contact Person`, "</p>"
    )
    )

# Define UI for application
ui <- fluidPage(

    # Application title
    titlePanel("Covid19 Vaccination Centers in India"),

    # Sidebar with a Dropdown 
    sidebarLayout(
        
        sidebarPanel(
            selectInput(inputId = "state_selection",
                        label = "Select State", 
                        choices = ind_vaccination_center$`State*`),
            
            h3("List of Vaccination Centers is plotted on Map & also listed in searchable table."),
                     
                     "source of list:",
                     a("https://www.timesnownews.com/india/article/covid-19-vaccination-in-uttar-pradesh-check-complete-list-of-govt-and-private-hospitals-for-jab/726412"),
                     br(),
                     br(),

                     a("https://www.oneindia.com/india/full-list-of-private-hospitals-where-the-covid-19-vaccine-will-be-administered-3223706.html"),
                     br(),
                     br(),
                     "P.S - There might be more center's added to this list, kindly recheck from other sources as well like:",
                     br(),
                     a("https://www.cowin.gov.in/home")
                     ),
        

        
        
        # Show Map & table
        mainPanel(
            tabsetPanel(type = "tabs",
                        tabPanel("Map", leafletOutput("map",height = 800, width = "100%")),
                        tabPanel("Data Table", tableOutput("mytable"))
        )
    )
)
)

# Define server logic 
server <- function(input, output) {
    
    # solution from: 
    
    output$map <- renderLeaflet({
        
        req(input$state_selection)
        data <- ind_vaccination_leaflet %>% 
            filter(`State*` == input$state_selection)
        
        # Creating map object & adding layers
        leaflet(data) %>%
            
            setView(lat = 26.64510, lng = 80.17012, zoom = 4) %>%
            addTiles(group = "OSM") %>%
            addProviderTiles(providers$CartoDB.DarkMatter, group = "Dark") %>%
            addProviderTiles(providers$CartoDB.Positron, group = "Light") %>%
            addProviderTiles("Stamen.Terrain", group = "Terrain") %>%
            addProviderTiles("Esri.WorldImagery", group = "WorldImagery") %>%
            addLayersControl(baseGroups = c("OSM","WorldImagery","Dark","Light","Terrain")) %>%
            addCircleMarkers(
                lng = ~`Longitude*`,
                lat = ~`Latitude*`,
                label = lapply(data$label_display, htmltools::HTML),
                color = "midnightblue",
                weight = 1,
                radius = 8
            )%>%
            addMiniMap(tiles = providers$OpenStreetMap, width = 120, height=80)
        
    })
    
    output$mytable = DT::renderDataTable({
        
        req(input$state_selection)
        data <- ind_vaccination_leaflet %>% 
            filter(`State*` == input$state_selection)
        
        data
    })
    
}


# Run the application 
shinyApp(ui = ui, server = server)

您需要进行两项更改。

tabPanel("Data Table", dataTableOutput("mytable"))

output$mytable = DT::renderDataTable({
    req(input$state_selection)
    data <- ind_vaccination_leaflet %>% 
      filter(`State*` == input$state_selection)
    
    datatable(data)
  })