How to share dataframes from an observeEvent module to another

shiny
shinydashboard
shiny-modules
#1

I need to share more than one dataframe within an observeEvent block with other observeEvent blocks. The reason is because the data is built only after a button is pressed.

I found the following question related, but not exactly what I need (to share a whole dataframe is different) ...

Using value from one shiny module to another shiny module

I tried to declare a dataframe as a variable at the beginning of the server module, but it will not change values when the code executes. I also tried to wrap the button observeEvent within a module, but then the app does not work. I cannot figure out how to change my code into modules to make it work. Your help will be greatly appreciated!

Here is a minimal example ...

library(shiny)
library(shinydashboard)
library(DT)

header1 <- dashboardHeader(
  title = "My App"
)

sidebar1 <- dashboardSidebar(
  sidebarMenu(id = "sbmenu",
              menuItemOutput("menuitems01"),
              menuItemOutput("menuitems02")
  ) #sidebarMenu
) #dashboardSidebar

body1 <- dashboardBody(
  tabItems(
    uiOutput("tabitems01")
  ) #tabItems
) #dashboardBody

ui <- dashboardPage(header1, sidebar1, body1)

server <- function(input, output, session) {

  # render menu
  output$menuitems01 <- renderMenu({
    menuItem("Main", tabName = "main", icon = icon("key"))
  })

  # render tabitems
  output$tabitems01 <- renderUI({
    tabItem(tabName = "main",
            h2("Main"),
            actionButton(inputId = "btn1", label = "Button1")
    ) #tabItem
  }) #renderUI

  observeEvent(input$btn1, {
    dfresult02 <- data.frame(c(1, 2), c(3, 4)) # e.g. read some data from db
    dfresult05 <- data.frame(c(5, 6), c(7, 8)) # e.g. read some data from db
    rResult02 <- reactive({dfresult02}) # NEED TO MAKE THIS DATA AVAILABLE TO OTHER MODULE(S)
    rResult05 <- reactive({dfresult05}) # NEED TO MAKE THIS DATA AVAILABLE TO OTHER MODULE(S)
    output$menuitems02 <- renderMenu({
      menuItem("MyData", tabName = "mydata", icon = icon("th"))
    }) #renderMenu
    updateTabItems(session, "sbmenu", "mydata")
    print("button1 pressed")
  }) #observeEvent(input$btn1)

  observeEvent(input$sbmenu, {

    # IF I UNCOMMENT THE NEXT FOUR LINES, THE TABLES ARE DISPLAYED
    #dfresult02 <- data.frame(c(1, 2), c(3, 4))
    #rResult02 <- reactive({dfresult02})
    #dfresult05 <- data.frame(c(1, 2), c(3, 4))
    #rResult05 <- reactive({dfresult05})

    if(input$sbmenu == "mydata")
    {
      output$tabitems01 <- renderUI({
        tabItem(tabName = "mydata",
                h2("My Data"),
                DT::dataTableOutput('tbl02'),
                DT::dataTableOutput('tbl05')
        ) #tabItem
      }) #renderUI
      output$tbl02 <- DT::renderDataTable({rResult02()}) # NEED DATA FROM OTHER MODULE HERE
      output$tbl05 <- DT::renderDataTable({rResult05()}) # NEED DATA FROM OTHER MODULE HERE
    } #if(input$sbmenu == "mydata")

    if(input$sbmenu == "main")
    {
      output$tabitems01 <- renderUI({
        tabItem(tabName = "main",
                h2("Main"),
                actionButton(inputId = "btn1", label = "Button1")
        ) #tabItem
      }) #renderUI
    } #if(input$sbmenu == "main")

  }) #observeEvent(input$sbmenu)

} #server

shinyApp(ui = ui, server = server)
0 Likes

closed #2

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.

0 Likes