【问题标题】:Reactive graph dependent on multiple inputs依赖于多个输入的反应图
【发布时间】:2020-01-02 16:17:28
【问题描述】:

我有一个反应图,它接受多个输入,但它依赖于所有这些输入。有没有一种方法可以让图表获取所有输入,但它并不依赖于所有输入。例如,如果用户选择一个下拉菜单,则图形将更新并且不需要任何其他输入,但如果用户添加第二个输入,则图形将使用第二个输入更新,但不需要第三个输入,除非它被选中。此外,如果用户选择的内容为 null,则不会更改图表。

用户界面:

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")
    })
}

我希望能够允许用户选择一个输入,它会更新图表,并且他们添加的图表越多,图表就会不断更新。

【问题讨论】:

    标签: shiny


    【解决方案1】:

    不是最整洁的,但是如何分离过滤如下:

    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(...)

    【讨论】:

    • 这正是我所需要的,不过只是一个问题。当我第一次打开闪亮的应用程序时,饼图会自动创建其中的所有输入,我如何获取它以便在用户选择输入之前它不会显示?
    • 感谢您的帮助,非常感谢!
    猜你喜欢
    • 2021-07-27
    • 2018-11-07
    • 1970-01-01
    • 1970-01-01
    • 2017-09-09
    • 1970-01-01
    • 2015-09-21
    • 2021-05-23
    • 1970-01-01
    相关资源
    最近更新 更多