簡體   English   中英

使用dplyr將輸入從Shiny傳遞到密謀時filter_imp中的錯誤

[英]Error in filter_imp when passing input from Shiny to plotly using dplyr

我正在開發Shiny應用程序,但無法理解錯誤。 我在下面創建了一個更簡單的可重現的示例來捕獲問題。 最終,我只想將city選擇框中的輸入傳遞給dplyr::filter() 然后,我想獲取過濾后的數據集並使用ggplot2對其進行ggplot2 最后,我想使用ggplotly() (來自plotly包)將靜態圖轉換為交互式圖。 下面的代碼顯示了我如何構建應用程序,並且在運行時出現了兩個不同的錯誤。

第一個錯誤是Warning: Error in filter_impl: Result must have length 9, not 0 長度9部分取決於數據,因此對於我的大型應用程序,錯誤是相同的,但長度為9721。

我也收到此錯誤, Warning in origRenderFunc() : Ignoring explicitly provided widget ID "154376613f66f"; Shiny doesn't use them Warning in origRenderFunc() : Ignoring explicitly provided widget ID "154376613f66f"; Shiny doesn't use them

可能最令人困惑的部分是,我的應用程序在拋出這兩個錯誤后可以正常工作,並且所有功能似乎都能正常工作。 如果您復制並粘貼下面的代碼並運行它,則該應用將在渲染圖輸出之前啟動並引發錯誤。

我正在嘗試構建一個顯示某些美國城市的唯一折線圖的應用程序。 我希望用戶界面可以選擇一個州,然后將城市選項過濾到該州內的那些城市。 下面的代碼產生所需的接口,但有兩個錯誤。 有人有見識嗎? 由於該應用程序可以正常運行並且具有我所追求的功能,因此很想抑制該錯誤消息,但我寧願了解造成問題的原因並進行修復。 提前謝謝了!

library(tidyverse)
library(shiny)
library(plotly)

df <- tibble(
  city = c(rep("city1", 3), rep("city2", 3), rep("city3", 3)),
  state = c(rep("state1", 6), rep("state2", 3)),
  year = c(rep(c(2016, 2017, 2018), 3)),
  dummy_value = c(1, 2, 3, 6, 7, 8, 15, 16, 17)
)

shinyApp(
  ui = fluidPage(
    sidebarLayout(
      sidebarPanel(
        selectInput(inputId = "state", 
                    label = "", 
                    choices = c("state1", "state2")),

        uiOutput("selected_city")

      ),
      mainPanel(
        plotlyOutput("example_plot")
      )
    )
  ),
  server = function(input, output, session) {
    output$selected_city <- renderUI({
      selectInput(inputId = "city", 
                  label = "", 
                  choices = dplyr::pull(
                  unique(df[df$state == input$state, "city"]), city)
                                  )
    })

    output$example_plot <- renderPlotly({
      plot_to_show <- plotly::ggplotly(
        df %>% 
          dplyr::filter(city == input$city) %>% 
          ggplot() +
          geom_point(aes(x = year, y = dummy_value))
      )
    })

  }
)

我不確定第二個警告(我沒有得到),但是對於第一個警告,只需使用以下命令:

output$example_plot <- renderPlotly({

  if(is.null(input$city))
    return()

 plotly::ggplotly(
    df %>% 
      dplyr::filter(city == input$city) %>% 
      ggplot() +
      geom_point(aes(x = year, y = dummy_value))
  )
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM