observeEvent() with multiple inputs

Hi there,

I am trying to build a shiny web app consisting of multiple pages. I want the app to open a certain page if any of a number of different actionButtons are pressed.

I build an example of the structure (see below). Page 2 should be loaded if either B1 or B2 are pressed.

I used | as an operator to include the different inputs, however I run into the problem that R loads page 2 right away instead of page 1. If you seperate the inputs by , R returns the error env must be an environment.

Please note that in the actual application I want to include >2 inputs.

Any help or hint how to solve this would be greatly appreciated!

Thanks a lot :slight_smile:

library(shiny)

ui <- (htmlOutput("page"))
    
Page1 <- function(...) {
    div(class = 'container', id = "Page1",
            actionButton("B1", "B1"),
            actionButton("B2", "B2")
        )}

Page2 <- function(...) {
    div(class = 'container', id = "Page2",
        actionButton("B3", "B3")
    )}
 
render_page <- function(...,f, title = "Test") {
    page <- f(...)
    renderUI({
        fluidPage(page, title = title)
    })
}

server <- function(input, output) {
    
  output$page <- render_page(f = Page1)
  
  observeEvent(input$B1 | input$B2, 
               {output$page <- render_page(f = Page2)
  })
}

shinyApp(ui = ui, server = server)
1 Like
library(shiny)

Page1 <- div(class = 'container', id = "Page1",
    actionButton("B1", "B1"),
    actionButton("B2", "B2")
)
Page2 <- div(class = 'container', id = "Page2",
             actionButton("B3", "B3"))
             
ui <- fluidPage(
  conditionalPanel("output.page_to_show == 'Page1'",
                   Page1),
  conditionalPanel("output.page_to_show == 'Page2'",
                   Page2)
)


server <- function(input, output) {
  page_to_show <- reactiveVal("Page2")
  
  output$page_to_show <- reactive(page_to_show())
  
  outputOptions(output, "page_to_show", suspendWhenHidden = FALSE)
  

  observeEvent(c(input$B1 , input$B2),
               {
                 page_to_show("Page2")
               })
  observeEvent(input$B3,
               {
                 page_to_show("Page1")
               })
}

shinyApp(ui = ui, server = server)
1 Like

Wow, thanks for this super fast and helpful reply! This is exactly what I was looking for. :slight_smile:

This topic was automatically closed 7 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.