将 Shiny 整合到 Flexdashboard 中以使用最少的 shiny 代码实现动态文本输入

Incorporating Shiny into Flexdashboard for dynamic textInput with the least shiny code

我构建了一个静态 Flexdashboard,其中包含 10 多个选项卡,每个选项卡中有多个图表。

我为每个被评估的人设置了一个变量作为搜索键来编织整个仪表板。

例如

library(flexdashboard)
library(tidyverse)
library(plotly)
library(DT)
library(cowplot)
library(lubridate)

df <- read_csv("Database.csv")
name <- regex(pattern="Smith") 

如果我想为一个叫 Virk 的人编一个评价,我只需要将名字 "Smith" 更改为 "Virk",而不必写完整的名字。它还从其他一些可能没有完整名称的 csv 中提取,因此首字母大写的姓氏是目前最好的解决方案。谢天谢地,我暂时没有两个相同的唯一名称。

我的另一个 tabs/plots 有一个使用

的通用过滤器
df_radar <- cccdb %>%
  filter(str_detect(Name,name))

我有超过 50 个图和数据表使用这个过滤系统。每个 html 文件(每个评估个体)的大小约为 6mb。

我想知道是否有更简单的方法可以使用 Shiny 的 textInput 动态更改我的绘图和数据表上的变量 "name" 和 renderPlot{},而无需更改我的整个仪表板代码。例如:

library(flexdashboard)
library(tidyverse)
library(plotly)
library(DT)
library(cowplot)
library(lubridate)
library(shiny)

df <- read_csv("Database.csv")
#Replacement of "name" variable
textInput("Name", "name:")
#one of the plot
renderPlot({
name <- regex(pattern = input$Name)
df_radar <- cccdb %>%
  filter(str_detect(Name,name) %>%
ggplot(.,
aes(x=Productivity)) +
geom_bar()

df_radar

})

我做到了。然而,问题是,我不得不重复这个:

name <- regex(pattern = input$Name)

所有 50 多个 renderPlots 和 renderDataTable。我想知道是否有更简单的方法。感谢您的帮助!

不好意思,刚学了shiny的reactive函数。我能够回答我自己的问题。我想出的解决方案是

# Replacement of name variable
textInput("name", "Name:")

# using shiny reactive
name <- reactive({
namer <- regex(pattern = input$name)
namer
})
#one of the plot
plotOutput("plot")

output$plot <- renderPlot({
df_radar <- cccdb %>%
  filter(str_detect(Name,name())) %>%
ggplot(.,
aes(x=Productivity)) +
geom_bar()

df_radar

})

对于name变量的其余部分,我只需要将它们替换为name()