【发布时间】:2019-06-07 08:30:48
【问题描述】:
我想在选择任何过滤器之前显示完整数据,然后根据过滤器选择对数据进行子集化。
例如: - 如果未选择任何内容,则应显示所有记录 - 如果只选择了大洲并且没有选择国家和州过滤器,那么它应该显示该特定大洲的所有记录 - 如果选择大陆和国家,那么它应该显示所选大陆和所选国家的所有记录 - 如果选择了洲、国家和州过滤器,那么它应该只显示所选过滤器的那些选项的记录
这是我的代码和数据集的链接 https://www.dropbox.com/s/5ii7fkt4anedjpb/R%20Codes.zip?dl=0
library(shinyjs)
library(stats)
library(shinydashboard)
library(sqldf)
library(shiny)
library(ggplot2)
library(stats)
library(lubridate)
library(DT)
library(sqldf)
setwd('C:\\Users\\folder')
header <- dashboardHeader(
title = "Shiny Dashboard")
sidebar <- dashboardSidebar(
sidebarMenu(
menuItem("Data", tabName = "ShowData", icon = icon("dashboard")),
menuItem("Summary", tabName = "ShowSummary", icon = icon("bar-chart-o"))
)
)
body <- dashboardBody(
useShinyjs(),
tabItems(
tabItem(tabName = "ShowData",
DT::dataTableOutput("table")
),
tabItem(tabName = "ShowSummary",
box(width =3,
h3("Tutorial by TopBullets.com"),
helpText("Please Continent, Country and State Combition"),
uiOutput("continent"),
uiOutput("country"),
uiOutput("state")
),
box(width =9,
DT::dataTableOutput("table_subset")
)) ))
# Put them together into a dashboardPage
ui <- dashboardPage(header,sidebar,body)
options(shiny.maxRequestSize = 15*1024^2)
server <- function(input,output){
# Importing data and save it temporary in data variable
data <- reactive({
read.table(file = "C:\\Users\\folder\\Countries-Continents-csv.csv",
sep = ",", header = T,
stringsAsFactors = F)
})
# Showing the original data
output$table <- DT::renderDataTable({
if(is.null(data())){return()}
DT::datatable(data(), options = list(scrollX = T))
})
# Creating filters
output$continent <- renderUI({
selectInput(inputId = "Continent", "Select Continent",choices = var_continent(), selected = "Asia")
})
output$country <- renderUI({
selectInput(inputId = "Country", "Select Country",choices = var_country(), selected = "India")
})
output$state <- renderUI({
selectInput(inputId = "State", "Select State",choices = var_state(),selected = "Goa")
})
# Cascasing filter for state
var_continent <- reactive({
file1 <- data()
if(is.null(data())){return()}
as.list(unique(file1$Continent))
})
# Creating reactive function to subset data
continent_function <- reactive({
file1 <- data()
continent <- input$Continent
file2 <- sqldf(sprintf("select * from file1 where Continent = '%s' ", continent))
return (file2)
})
var_country <- reactive({
file1 <- continent_function()
if(is.null(file1)){return()}
as.list(unique(file1$Country))
})
state_function <- reactive({
file1 <- continent_function()
country <- input$Country
file2 <- sqldf(sprintf("select * from file1 where Country = '%s' ", country))
return (file2)
})
var_state <- reactive({
file1 <- state_function()
as.list(unique(file1$State))
})
output$table_subset <- DT::renderDataTable({
file1 <- state_function()
state <- input$State
file2 <- sqldf(sprintf("select * from file1 where State = '%s' ", state))
DT::datatable(file2, options = list(scrollX = T))
})
}
shinyApp(ui,server)
【问题讨论】:
标签: r filter shiny shinydashboard reactive