0

I have an R shiny app using golem to generate a report on NGS data. I currently have a working app that reads in data (some excel files and a pdf) and shows the graphs and tables relating to that input. I would like to have the app start with a drop down selection where I can choose which projects data will be analyzed. It will then read in the proper files which are stored in a directory matching the chosen project.

What I am having trouble with is making everything work in a reactive and dynamic way to update all of my analysis/results when I switch projects and therefore the inputs for these results. My understanding is that I need to pass reactive values (ex. dataframe) from one module where it is created to another where it is used but I cannot get any updates I make to reflect throughout the app.

I can show some code to show where what I am exploring now and explain the simplest minimal example of what I am trying to accomplish.

In this example I am only trying to pass on the selected project (coming from the Initialization module) and display it in the test module. Here I have the app server and app ui sections, as well as the two modules I am using. Initialization to pick the project and test to display it. This code works for running the app but the output never updates in the test module no matter what I do in the Initialization module.

app_server <- function(input, output, session) {
  rv = reactiveValues(projectid = NULL)

  initial = mod_Initialization_server("Initialization_1")
  mod_test_server("test_1", project = reactive({rv$projectid}))

  observeEvent(initial$trigger, {
    rv$projectid = initial$projectid
  })

app_ui <- function(request) {
  tagList(
    # Leave this function for adding external resources
    golem_add_external_resources(),
    shinydashboardPlus::dashboardPage(
      
      skin = "purple",
      header = shinydashboardPlus::dashboardHeader(
        title = "NGS Report Generator"
      ),
      
      sidebar = shinydashboard::dashboardSidebar( width = 200,
                                                  shinydashboard::sidebarMenu(
                                                    id = "tabs",
                                                    shinydashboard::menuItem("Home", tabName = "home"),
                                                    shinydashboard::menuItem("Basic Stats", tabName = "basic"))),
      body = shinydashboard::dashboardBody(
        shinydashboard::tabItems(
          shinydashboard::tabItem("home", mod_Initialization_ui("Initialization_1")),
          shinydashboard::tabItem("projinfo", mod_test_ui("test_1"))
        )
      )
    )
  )
}


mod_Initialization_ui <- function(id){
  ns <- NS(id)

  #This list needs to be generated by available projects
  ProjectList = c("---","X", "Y")

  tagList(
    fluidPage(
      fluidRow(
        column(width = 12,
          h2("Please select the project your report will be generated on")),
        br(),br(),br()
      ),
      fluidRow(
        selectInput(ns("project"), label = "", choices = ProjectList)
      )
    )
  )
}


mod_Initialization_server <- function(id){
  moduleServer( id, function(input, output, session){
    ns <- session$ns

    initializationrv = reactiveValues(
      projectid = "",
      trigger = 0
    )

    observeEvent(input$project,{
      initializationrv$project = input$project
      initializationrv$trigger = initializationrv$trigger +1
    })

    #project = reactive({input$project})
    return(initializationrv)
  })
}

mod_test_ui <- function(id){
  ns <- NS(id)
  tagList(shinyjs::useShinyjs(),
          fluidPage(h1(textOutput(ns("projectid"))))
  )
}

mod_test_server <- function(id, project = ""){
  moduleServer( id, function(input, output, session){
    ns <- session$ns

    observe(output$projectid <- renderText({
      project()
    }))
  })
}
shaberman
  • 1
  • 1
  • You seem to have some typos:1. `rv$projectid = initial$projectid` should be `rv$projectid = initial$project`. 2. `tabName` should match between `sidebar ` and `body`. – YBS Jan 26 '23 at 15:03
  • Thanks for giving it a look and yes I did make some typos copying this over since I was trying to cut in only a small part of the total code. For the tabName its because I am missing this line `shinydashboard::menuSubItem("Project Information", tabName = "projinfo")`. But the first typo you mentioned helped me solve this case! The mistake was actually in `initializationrv$project = input$project` where it should be projectid on the left side. – shaberman Jan 26 '23 at 15:18
  • I will try to adapt a larger part of my code and report back tomorrow. Thanks for giving it a look. – shaberman Jan 26 '23 at 15:26
  • Just wanted to report back with a final update. I was able to get my app running smoothly. The main things that were helpful to me and that I couldn't find on SO was setting global reactive values that I passed along to each module and that were updated with triggers, as well as updating my reactive values in each module with observeEvent when the triggers were activated. – shaberman Jan 31 '23 at 15:33

0 Answers0