【问题标题】:Trying to create a reactive graph that will take in multiple different inputs in shiny尝试创建一个反应图,该图将在闪亮中接受多个不同的输入
【发布时间】:2020-01-01 14:02:05
【问题描述】:

我在侧边栏和主体中向闪亮的应用程序添加了多个选择输入,并希望创建一个图表,当任何这些输入被选择或更改时会发生变化,但我不断收到错误警告:错误: 结果的长度必须是 56127,而不是 0。

用户界面:

ui <- dashboardPage(
    dashboardHeader(title = "Human Trafficking"),

    dashboardSidebar(
        sidebarMenu(
            selectInput("Source", "Choose a Data Source: ", choices = " ", 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 = " "),
            actionButton("button", "Apply")
        )
    ),

    dashboardBody(

        fluidRow(
            box(width = 4, solidHeader = TRUE,
                selectInput("traffickingType", "Choose a trafficking type: ", choices = " ", selected = NULL,
                            multiple = TRUE, selectize = TRUE, width = NULL, size = NULL)
            ),
            box(width = 4, solidHeader = TRUE,
                selectInput("traffickingSubType", "Choose a trafficking sub type: ", choices = " ", selected = NULL,
                            multiple = TRUE, selectize = TRUE, width = NULL, size = NULL)
            ),
            box(width = 4, solidHeader = TRUE,
                selectInput("gender", "Choose a gender: ", choices = " ", selected = NULL,
                            multiple = TRUE, selectize = TRUE, width = NULL, size = NULL)
            )
        ),
        fluidRow(
            box(width = 12,
                plotlyOutput('coolplot')
            )
        )
    )
)

服务器:

server <- function(input, output, session) {

  genderVic = sort(unique(ngo$Victim.Gender))
  updateSelectInput(session, "gender", choices = genderVic)

  traffickingSub = sort(unique(ngo$Trafficking.Sub.Type))
  updateSelectInput(session, "traffickingSubType", choices = traffickingSub)

  trafficking = sort(unique(ngo$Trafficking.Type))
  updateSelectInput(session, "traffickingType", choices = trafficking)

  traffickerNationalities = sort(unique(ngo$Trafficker.Nationality))
  updateSelectInput(session, "TraffickerNation", choices = traffickerNationalities)

  dataSource = sort(unique(ngo$Data.Provided.By))
  updateSelectInput(session, "Source", choices = dataSource)

  nationalities = sort(unique(ngo$Victim.Nationality))
  updateSelectInput(session, "Nationality", choices = nationalities)

  output$coolplot <- renderPlotly({
    ngo <-
      ngo %>%
      filter(Victim.Nationality == input$Nationality,
             Victim.Gender == input$gender,
             Trafficking.Type == input$traffickingType
      )

    p = ggplot(ngo, aes(x = Victim.Age, fill = Trafficking.Type)) + 
      geom_bar(position = "stack")
    ggplotly(p) %>%
      layout(showlegend = FALSE)
  })
}

所以目前它只调用三个输入来测试它,但仍然出现错误。

【问题讨论】:

    标签: r ggplot2 shiny dplyr plotly


    【解决方案1】:

    在您的示例中选择性别和广告投放类型后,它应该会在显示错误后起作用。错误的原因是 renderPlotly 期望 input$traffickingTypeinput$gender 的值,但这些值以 NULL 开头。

    为每个selectInputs 添加一个req

        output$coolplot <- renderPlotly({
            ngo <-
                ngo %>%
                filter(Victim.Nationality == input$Nationality,
                       Victim.Gender == req(input$gender),
                       Trafficking.Type == req(input$traffickingType)
                )
    
            p = ggplot(ngo, aes(x = Victim.Age, fill = Trafficking.Type)) + 
                geom_bar(position = "stack")
            ggplotly(p) %>%
                layout(showlegend = FALSE)
        })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-05
      • 2018-10-09
      • 2020-10-27
      • 1970-01-01
      • 2018-05-28
      • 2017-02-23
      相关资源
      最近更新 更多