[英]Incorporating Shiny into Flexdashboard for dynamic textInput with the least shiny code
我已經構建了一個 static 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 中提取,因此第一個字母為大寫的姓氏是目前最好的解決方案。 謝天謝地,我現在沒有兩個相同的唯一名稱。
我的其他選項卡/繪圖有一個通用過濾器,使用
df_radar <- cccdb %>%
filter(str_detect(Name,name))
我有超過 50 多個使用這個過濾器系統的圖和數據表。 每個 html 文件(每個評估的個體)大小約為 6mb。
我想知道是否有一種更簡單的方法可以使用 Shiny 的 textInput 在我的繪圖和數據表上動態更改變量“名稱”和 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 多個渲染圖和渲染數據表。 我想知道是否有更簡單的方法。 謝謝您的幫助!
抱歉,我剛剛學習了 shiny 的反應式 function。 我能夠回答我自己的問題。 我想出的解決方案是
# 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變量的rest,我只需要將它們替換為name()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.