在我 Shiny 的应用程序中,我有一个日期范围输入和一组复选框。复选框选择是根据 input$dateRange 确定的。我遇到了一个问题,每次更改日期范围时,绘图都会重绘两次。第一次重绘时,它将使用新的日期范围,但使用旧的复选框选择。然后复选框选项会更新,绘图会再次重绘。
有什么方法可以防止多次重绘绘图,并且只有在所有其他 UI 元素都更新后才绘制它?
server.R 代码片段
# Check boxes for variants in run
output$choose_variants <- renderUI({
# Get the variants associated with the run
dat <- loadVariants(input$dateRange[1], input$dateRange[2])
if(is.null(dat))
return()
# Create the checkboxes and select them all by default
checkboxGroupInput("variants", "Variants",
choices = dat$variant,
selected = dat$variant)
})
# Output the data
output$plot1 <- renderPlot({
runLocations <- loadRunsBetweenDates(input$dateRange[1], input$dateRange[2], input$variants)
#ggplot()
})
ui.R 代码片段
sidebarPanel(
dateRangeInput('dateRange',
label = 'Date range',
start = Sys.Date(), end = Sys.Date()
),
uiOutput("choose_variants")
),
最佳答案
由于 input$variants
总是在日期范围滑块更改时更改,因此您可以使绘图仅依赖于 input$variants
。
# Output the data
output$plot1 <- renderPlot({
# outside of isolate statement, so this plot becomes dependent on its value.
input$variants
# put this in an isolate block, so it doesn´t trigger an invalidation.
isolate(runLocations <- loadRunsBetweenDates(input$dateRange[1], input$dateRange[2], input$variants))
#ggplot()
})
希望这可以帮助!
您可以创建一个响应式(Reactive),并使您的情节仅依赖于它,如下所示:
loadedVariants <- reactive({
loadVariants(input$dateRange[1], input$dateRange[2])
})
# Output the data
output$plot1 <- renderPlot({
# outside of isolate statement, so this plot becomes dependent on its value.
loadedVariants()
# put this in an isolate block, so it doesn´t trigger an invalidation.
isolate(runLocations <- loadRunsBetweenDates(input$dateRange[1], input$dateRange[2], input$variants))
#ggplot()
})
关于r - 如何防止每次 UI 交互多次重绘 Shiny 的情节?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45571048/