我正在尝试在两个动态链接的selectInput小部件中添加ALL选项,这些小部件将过滤到主面板中的图形。基本上,我希望用户选择服务,例如在线或邮购,或所有服务,以及与Prof.Los列类似,我希望用户选择利润或亏损,或两者之和。
当两个输入动态链接时,我不确定如何添加此额外选项。所以我的问题是,如何在两个下拉菜单中都包含一个选项,以按所有变量过滤数据?
我的图书馆和数据:
library("shiny")
library("plotly")
library("tidyverse")
library("dplyr")
data <- data.frame(Services = c("Online","Online","Online","Online","Online","Online","Online","Online","Online", "Online","Online","Online","Mail-order","Mail-order","Mail-order","Mail-order","Mail-order","Mail-order","Mail-order","Mail-order","Mail-order","Mail-order","Mail-order","Mail-order","Shop","Shop","Shop","Shop","Shop","Shop","Shop","Shop","Shop","Shop","Shop","Shop"),
Month = c("2013-01-01","2013-02-01","2013-03-01","2013-04-01","2013-05-01","2013-06-01","2013-07-01","2013-08-01","2013-09-01","2013-10-01","2013-11-01","2013-12-01","2013-01-01","2013-02-01","2013-03-01","2013-04-01","2013-05-01","2013-06-01","2013-07-01","2013-08-01","2013-09-01","2013-10-01","2013-11-01","2013-12-01","2013-01-01","2013-02-01","2013-03-01","2013-04-01","2013-05-01","2013-06-01","2013-07-01","2013-08-01","2013-09-01","2013-10-01","2013-11-01","2013-12-01"),
Sales = c(40,50,20,30,40,50,200,100,250,100, 120,130,40,80,20,30,30,50,400,100,150,100,75,50,100,50,700,30,40,50,100,120,220,100,75,150),
Prof.Los = c("Profit","Loss","Profit","Profit","Loss","Loss","Loss","Profit","Profit","Loss","Loss","Profit","Profit","Loss","Profit","Loss","Profit","Loss","Loss","Loss","Profit","Loss","Loss","Profit","Profit","Profit","Loss","Loss","Loss","Profit","Profit","Loss","Loss","Loss","Profit","Loss"))
我的代码:
UI
ui <- fluidPage(
# App title ----
titlePanel(h1("Analyser tool")),
# Sidebar layout with input and output definitions ----
sidebarLayout(
sidebarPanel(
# Input: Select service type ----
uiOutput("services"),
uiOutput("rev")
),
# Main panel for displaying outputs ----
mainPanel(
# Output: Tabset w/ plot, summary, and table ----
tabsetPanel(type = "tabs",
tabPanel("Bar Chart", plotlyOutput("Plot", height = "450px"))
)
)
))
服务器
server <- function(input, output) {
# Service analyser reactive dataset
output$services = renderUI({
selectInput("services",
"Select services:",
choices = unique(data$Services))
})
ServiceSub <- reactive({
data %>%
filter(Services == input$services)
})
output$rev = renderUI({
selectInput(inputId = "rev",
label = "Profit/loss",
choices = unique(ServiceSub()[,"Prof.Los"]),
selected = unique(ServiceSub()[,"Prof.Los"]))
})
RevSub <- reactive({
req(input$services)
filter(ServiceSub(), Prof.Los %in% input$rev)
})
output$Plot = renderPlotly({
# plotly code
plot_ly(RevSub(), x = ~Month, y = ~Sales, type = "bar")
})
}
# Create Shiny app ----
shinyApp(ui, server)