我有一个反应图,它接受多个输入,但它取决于所有这些输入。有没有一种方法可以使图形接受所有输入,但并不依赖于所有输入。例如,如果用户选择一个下拉列表,则该图将更新并且不需要任何其他输入,但是如果用户添加第二个输入,则该图将使用第二个输入进行更新,但不需要第三个输入,除非已选择它。另外,如果用户选择的内容为null,则不会更改图形。
UI:
ui <- dashboardPage(
dashboardHeader(title = "Human Trafficking"),
dashboardSidebar(
sidebarMenu(
selectInput("Source", "Choose a Data Source: ", choices = sort(unique(ngo$Data.Provided.By)), selected = NULL,
multiple = TRUE, selectize = TRUE, width = NULL, size = NULL),
dateInput("startdate", "Start Date:", value = "2009-01-01", format = "dd-mm-yyyy",
min = "2009-01-01", max = "2019-08-26"),
dateInput("enddate", "End Date:", value = "2019-08-27", format = "dd-mm-yyyy",
min = "2009-01-02", max = "2019-08-27"),
selectInput("Nationality", "Select a nation: ", choices = sort(unique(ngo$Victim.Nationality))),
actionButton("button", "Apply")
)
),
dashboardBody(
fluidRow(
box(width = 4, solidHeader = TRUE,
selectInput("traffickingType", "Choose a trafficking type: ", choices = sort(unique(ngo$Trafficking.Type)), selected = NULL,
multiple = TRUE, selectize = TRUE, width = NULL, size = NULL)
),
box(width = 4, solidHeader = TRUE,
selectInput("traffickingSubType", "Choose a trafficking sub type: ", choices = sort(unique(ngo$Trafficking.Sub.Type)), selected = NULL,
multiple = TRUE, selectize = TRUE, width = NULL, size = NULL)
),
box(width = 4, solidHeader = TRUE,
selectInput("gender", "Choose a gender: ", choices = sort(unique(ngo$Victim.Gender)), selected = NULL,
multiple = TRUE, selectize = TRUE, width = NULL, size = NULL)
)
),
fluidRow(
box(width = 6, solidHeader = TRUE, status = "primary",
title = "Trafficking Type",
plotlyOutput("coolplot", width = '750px', height = '600px')
),
box(width = 6, solidHeader = TRUE, status = "primary",
title = "Trafficking Sub-Type",
plotlyOutput("Sub", width = '750px', height = '600px')
)
)
)
)
服务器:
server <- function(input, output, session) {
output$coolplot <- renderPlotly({
ngo <-
ngo %>%
filter(Victim.Nationality %in% input$Nationality,
Victim.Gender %in% input$gender,
Trafficking.Type %in% input$traffickingType,
Trafficking.Sub.Type %in% input$traffickingSubType,
Data.Provided.By %in% input$Source
) %>%
plot_ly(labels = ~Trafficking.Type, type = "pie")
})
}
我希望能够允许用户选择一个输入,它将更新图形,并且他们添加的图形越多,则将不断更新。
答案 0 :(得分:0)
不是最巧妙的方法,但是如何分离过滤如下:
server <- function(input, output, session) {
output$coolplot <- renderPlotly({
req(c(input$gender, input$traffickingType, input$traffickingSubType))
if(!is.null(input$Nationality)) {
ngo <- ngo %>% filter(Victim.Nationality %in% input$Nationality)
}
if(!is.null(input$gender)) {
ngo <- ngo %>% filter(Victim.Gender %in% input$gender)
}
if(!is.null(input$traffickingType)) {
ngo <- ngo %>% filter(Trafficking.Type %in% input$traffickingType)
}
if(!is.null(input$traffickingSubType)) {
ngo <- ngo %>% filter(Trafficking.Sub.Type %in% input$traffickingSubType)
}
if(!is.null(input$Source)) {
ngo <- ngo %>% filter(Data.Provided.By %in% input$Source)
}
plot_ly(ngo, labels = ~Trafficking.Type, type = "pie")
})
}
shinyApp(ui, server)
更新
基于以下评论。
我添加了req(c(input$gender, input$traffickingType, input$traffickingSubType))
。
我在启动时遗漏了input$Nationality
,因为它等于“ A”,我假设是input$Source
,但是如果需要,可以将input$source
添加到上面的向量c(...)
中。