首页 > 解决方案 > 如何强制闪亮更新来自不同 Flex Dashboard 部分的所有反应元素?

问题描述

我有一个弹性仪表板,它有一个带有文本/复选框选项的侧边栏,可以更改数据。

我有 2 个不同的部分,第 2 部分中的情节仅在第 2 部分导航到时更新。这会在使用应用程序时产生滞后的体验,并且在下载数据时会产生不一致的情况,这会迫使用户导航到创建图表的副作用所在的每个部分。

我的目标是让两个图表立即连续呈现,而无需导航到不同的选项卡。

以下是在带有扩展名的 RStudio 中应使用shinyflexdashboardtidyversehtmlwidgets安装的示例代码:.Rmd

---
title: Example Dashboard
output:
  flexdashboard::flex_dashboard:
runtime: shiny
---
  
```{r setup, include=FALSE, echo=FALSE, message=FALSE, warning=FALSE}
library(tidyverse)
library(flexdashboard)
library(shiny)
library(htmlwidgets)

diamond_colors = sort(unique(diamonds$color))

pal = c('#14BAE6','#62B3EB','#8DABEC','#B2A1E5','#D198D4','#E98FC1','#FE85AD')
# global count statistics
cnt1 = 0
cnt2 = 0

filtered_data <- eventReactive(
  input$selected_colors,
  {
    return(diamonds %>% 
      filter(
        color %in% input$selected_colors
      ) %>%
        group_by(cut, clarity) %>%
        summarize(
          count=n(),
          avg_price = mean(price),
          avg_price_per_carat=median(price/carat)
        )
    )
  }
)

output$checkboxOptionInput <- renderUI({
  checkboxGroupInput(
    'selected_colors',
    label='Select Colors:',
    choices=diamond_colors,
    selected=diamond_colors
  )
})

output$section1Plot <- renderPlot({
  data = filtered_data()
  cnt1 <<- sum(data$count)
  ggplot(data) + 
    geom_tile(aes(x=cut,y=clarity,fill=avg_price)) +
    ggtitle('Average Price of Diamonds by Cut and Clarity') +
    scale_fill_gradientn(colors=pal)
})

output$section2Plot <- renderPlot({
  data = filtered_data()
  cnt2 <<- sum(data$count)
  ggplot(data) + 
    geom_tile(aes(x=cut,y=clarity,fill=avg_price_per_carat)) +
    ggtitle('Average Price of Diamonds per Carat by Cut and Clarity') +
    scale_fill_gradientn(colors=pal)
})

output$downloadData <- downloadHandler(
  filename = function() {'diamonds_example.csv'},
  content=function(file){
    write_csv(filtered_data() %>% mutate(cnt1=cnt1,cnt2=cnt2), file=file)
  }
)

output$downloadUI <- renderUI({
  downloadButton('downloadData','Download Stats')
})

```

Section 1
===
  
Options {.sidebar}
------------------------------------------------
```{r}
uiOutput('checkboxOptionInput')
uiOutput("downloadUI")
```

Row {}
------------------------------------------------
```{r}
plotOutput('section1Plot', height=720)
```

Section 2
===
  
Row {}
-----------------------------------------------
```{r}
plotOutput('section2Plot', height=720)
```

我将计数统计信息包括在内,以检查用于生成第一个和第二个图表的最后一个数据的行数。

标签: rshinyflexdashboard

解决方案


推荐阅读