[英]Incorporating Shiny into Flexdashboard for dynamic textInput with the least shiny code
I have built a static Flexdashboard with 10+ tabs and multiple plots in each tab.我已经构建了一个 static Flexdashboard,其中包含 10 多个选项卡和每个选项卡中的多个绘图。
I set a variable as a search key for each evaluated individual to knit the entire dashboard.我将一个变量设置为每个评估个体的搜索键,以编织整个仪表板。
For example例如
library(flexdashboard)
library(tidyverse)
library(plotly)
library(DT)
library(cowplot)
library(lubridate)
df <- read_csv("Database.csv")
name <- regex(pattern="Smith")
I just need to change the name "Smith" to "Virk" if I want to knit an evaluation for a person named Virk, without having to entire the whole name.如果我想为一个名叫 Virk 的人进行评估,我只需将名称“Smith”更改为“Virk”,而不必填写整个名字。 It also pulls from a few other csv that may not have the entire name, hence the last name with the first letter being uppercase is the best solution for now.
它还从其他一些可能没有完整名称的 csv 中提取,因此第一个字母为大写的姓氏是目前最好的解决方案。 I don't have 2 of the same unique name for now, thank goodness.
谢天谢地,我现在没有两个相同的唯一名称。
My other tabs/plots have a generic filter using我的其他选项卡/绘图有一个通用过滤器,使用
df_radar <- cccdb %>%
filter(str_detect(Name,name))
I have over 50+ plots and datatables using this filter system.我有超过 50 多个使用这个过滤器系统的图和数据表。 Each html file (each evaluated individual) is about 6mb in size.
每个 html 文件(每个评估的个体)大小约为 6mb。
I am wondering if there is an easier way to use Shiny's textInput for dynamic change of variable "name" and renderPlot{} on my plots and datatables without having to change my entire dashboard code.我想知道是否有一种更简单的方法可以使用 Shiny 的 textInput 在我的绘图和数据表上动态更改变量“名称”和 renderPlot{},而无需更改我的整个仪表板代码。 For example:
例如:
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
})
I was able to do that.我能够做到这一点。 However, the problem is, I'd have to repeat this:
然而,问题是,我不得不重复这个:
name <- regex(pattern = input$Name)
for all 50+ renderPlots and renderDataTable.适用于所有 50 多个渲染图和渲染数据表。 I was wondering if there is a simpler way.
我想知道是否有更简单的方法。 Thank you for your help!
谢谢您的帮助!
Sorry, I just learnt the reactive function of shiny.抱歉,我刚刚学习了 shiny 的反应式 function。 I was able to answer my own question.
我能够回答我自己的问题。 The solution I had come up with is
我想出的解决方案是
# 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
})
For the rest of the name variable, I just need to replace them with name()对于name变量的rest,我只需要将它们替换为name()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.