r - 如何强制闪亮更新来自不同 Flex Dashboard 部分的所有反应元素?
问题描述
我有一个弹性仪表板,它有一个带有文本/复选框选项的侧边栏,可以更改数据。
我有 2 个不同的部分,第 2 部分中的情节仅在第 2 部分导航到时更新。这会在使用应用程序时产生滞后的体验,并且在下载数据时会产生不一致的情况,这会迫使用户导航到创建图表的副作用所在的每个部分。
我的目标是让两个图表立即连续呈现,而无需导航到不同的选项卡。
以下是在带有扩展名的 RStudio 中应使用shiny
、flexdashboard
、tidyverse
和htmlwidgets
安装的示例代码:.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)
```
我将计数统计信息包括在内,以检查用于生成第一个和第二个图表的最后一个数据的行数。
解决方案
推荐阅读
- haskell - 在单例数据类型中使用 Nat/Natural 的正确方法是什么?
- javascript - 打开模态的问题
- c++ - 如何修复 ld: symbol(s) not found for architecture x86_64?
- reactjs - 如何允许快速后端 REST API 在使用 axios 的反应前端中设置 cookie?
- python - 有没有办法检查生成列表中的单词是否在文本文件中?
- prolog - Prolog中发生检查的最坏情况是什么?
- android - 将 OnClick 侦听器从超级片段分配给新活动
- node.js - 尽管路径已填充,但 Mongoose 填充()返回未定义
- sql - Node.js express 和 postgress sql pool.query
- discord - 当用户对消息做出反应时,如何将值存储到变量中?