rshinyrenderuioutput

One reactive function to be displayed on two different pages interactively


I have an application which has 3 tabItems. I want to use a slider on second page to display same result on 3rd page interactively, i.e. if 2nd page slider changes then 3rd page slider should also change respectively.

I have a reactive function on server side

choose_segment <-  reactive({

  Multiple conditions for dropdown{Due to security cant share the exact code.}

  })

and this choose_segment is refered in UI once and now i want to use it on the third page as well, but when i am calling the function on third page it is not displaying any thing on ui and also not giving any error. in UI it is called inside UIoutput.

uiOutput(choose_segment())

My observations : I think as per my study we can not call one function directly twice, so what i am doing is i have made two different functions and calling same function from them, i.e.

output$chooseSegment1 <- renderUI({
  choose_segment()
  })
  output$chooseSegment2 <- renderUI({
    choose_segment()
  })

Issue : it is giving me output but they both are not interactive :(

Kindly provide a solution so that i can make both the sliders work in interactive manner.


Solution

  • I have faced the same scenario, in that i was suppose to change the code structure. I made dynamic output uiOutput to the Dropdown menu ob ui and then used the same in my server as Input$xyz in observe on server and it worked for me. Code :

    UI : column(3, selectInput(inputId="ABC",label= "Choose ABC"))
     column(3, selectInput(inputId="ABC1",label= "Choose ABC"))
    Server : observe({
    
        if(is.null(tab2_summary())) return(NULL)
        updateSelectInput(session, "ABC", value = input$ABC)
    
      })
    observe({
      updateSelectInput(session, "ABC1", value = input$ABC)
    
    })
    observe({
    
      updateSelectInput(session, "ABC", value = input$ABC1)
    
    })
    

    So this is how i was able to make the selectInput interactive on two different page. For your reference there is one full reproducible code. Kindly refer,

    library(shiny)
    # UI ----------------------------------------------------------
    ui <- navbarPage("Navbar!",
                     tabPanel("Plot", sidebarLayout(sidebarPanel(
                       radioButtons("yaxis1", "y-axis", c("speed"="speed", "dist"="dist"),
                                    selected = "speed"
                       )),
                       mainPanel( plotOutput("plot"),
                                  textOutput("test2")))),  # for input checking
    
                     tabPanel("Summary", sidebarLayout(sidebarPanel(
                       radioButtons("yaxis2", "grouping-var", c("speed"="speed", "dist"="dist")
                       )),
                       mainPanel(
                         verbatimTextOutput("summary"),
                         textOutput("test1")
                       )))
    )
    # Server ------------------------------------------
    server <- function(input, output, session) {
    
      observe({
        x <- input$yaxis1
        updateRadioButtons(session, "yaxis2", selected = x)
      })
    
      observe({
        y <- input$yaxis2
        updateRadioButtons(session, "yaxis1", selected = y)
      })
    
      # output$test1 <- renderPrint({cat("yaxis1", input$yaxis1)})
      # output$test2 <- renderPrint({cat("yaxis2", input$yaxis2)})
      # output$plot <- renderPlot({ plot(cars[['speed']], cars[[input$yaxis1]]) })
      # output$summary <- renderPrint({ summary(cars[[input$yaxis2]]) })
    }
    shinyApp(ui, server)
    

    I Hope it will of your help.