我想在选择任何过滤器之前显示完整数据,然后根据过滤器选择对数据进行子集化。
例如: -如果未选择任何内容,则应显示所有记录 -如果仅选择大洲,而未选择国家和州过滤器,则它应显示该特定大洲的所有记录 -如果选择了“大陆和国家/地区”,则它应显示选定大陆和选定国家/地区的所有记录 -如果选择了大陆,国家和州过滤器,则它应仅显示所选过滤器的那些选项的记录
这是我的代码,并链接到数据集 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)
答案 0 :(得分:1)
在“摘要”选项卡中,您可以在每个selectInput
的顶部添加“全部”条目,并且仅在选定的洲/国家/州不是“全部”的情况下进行过滤。为此,您需要:
as.list(c("All", unique(file1$Continent)))
然后获取大陆数据的反应函数如下所示:
continent_function <- reactive({
file1 <- data()
continent <- input$Continent
sql = "select * from file1"
if(continent != "All") {
sql = paste(sql, sprintf("where Continent = '%s' ", continent))
}
file2 <- sqldf(sql)
return (file2)
})
(并且类似地适用于国家和州。)