Search code examples
rshinyflexdashboard

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.

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. 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. 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. Each html file (each evaluated individual) is about 6mb in size.

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. 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. I was wondering if there is a simpler way. Thank you for your help!


Solution

  • Sorry, I just learnt the reactive function of shiny. 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()