【问题标题】:use multiple RenderUI under reactive()在 reactive() 下使用多个 RenderUI
【发布时间】:2020-12-15 16:30:52
【问题描述】:

我有闪亮的应用程序包含多个动态/条件输入(下一个输入基于我在前一个中选择的内容)。为此,我使用renderUI

output$input1_server <- renderUI({ 
validate(need(base(), "Error"))  
 
df <- base %>%
      filter(age >= 10) %>%
      select(city)

selectInput(session$ns("input1"), "Input #1:", 
            choices = df$city, 
            width = "100%")
})

我还有reactive,它使用输入作为过滤参数,例如:

data <- reactive({

base %>%
#some filters %>%
#some manipulations %>%
#etc 

})

然后我使用reactive data() 构建反应图

plot <- reactive({

data() %>%
ggplot( ... ) %>%
geom_bar( ... ) %>%
#etc...

})

# Rendering
output$plot_output <- renderPlot({ plot() })

当我更改动态输入时会出现问题,这会导致其他动态输入发生更改。然后出现renderPlot() 中的错误,尽管有错误,但在几秒钟内会出现图。我确信由于大量的renderUI而发生错误。我尝试使用sys.sleep 为函数执行提供更多时间,但它不起作用。代码是正确的,因为当我在闪亮的应用程序中使用它时它可以工作。

【问题讨论】:

  • 请做一个小(最小)示例,包括重现错误的示例数据。
  • 请提供minimal reproducible example,即显示此行为的完整闪亮应用程序。否则我不确定你的问题是什么。但是是的,一般来说,很多 renderUI 会减慢您的应用速度

标签: r shiny shiny-server shinyapps shiny-reactivity


【解决方案1】:

在没有最小可重复示例的情况下,我会给出最好的猜测。
您可以使用req 来确保data()plot() 可用:

plot <- reactive({
req(data())
data() %>%
ggplot( ... ) %>%
geom_bar( ... ) %>%
#etc...

})

# Rendering
output$plot_output <- renderPlot({ 
  req(plot)
  plot() })

另一个选项是validate:

plot <- reactive({
validate(need(nrow(data())>0,"Data not yet calculated"))
data() %>%
ggplot( ... ) %>%
geom_bar( ... ) %>%
#etc...

})

我希望这会有所帮助,但如果没有 MRE,很难说得更多。

【讨论】:

    猜你喜欢
    • 2015-01-19
    • 2018-07-04
    • 1970-01-01
    • 2019-12-18
    • 1970-01-01
    • 2017-06-29
    • 2019-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多