How to reuse the selectInput from uiOutput and renderUI ? (Within modularized App)

How can we reuse the uiOutput used for selectInput drop downs
to reuse the columns2 within Server function and display in UI ?

similar Q:

runApp(list(
  ui = bootstrapPage(
    selectInput('dataset', 'Choose Dataset', c('mtcars', 'iris')),
    uiOutput('x'),
    uiOutput('y'),
    plotOutput('plot')
  ),
  server = function(input, output){
    output$x= renderUI({
      mydata = get(input$dataset)
      selectInput('columns2', 'X axis', names(mydata))
    })
    output$y = renderUI({
      mydata = get(input$dataset)
      selectInput('columns3', 'Y axis', names(mydata))
    })
    output$plot = renderPlot({
      # How to use the x, y to plot something e.g Petal.Width vs Petal.Length
      plot(x,y)
    })
  }
))

Modularized version

Why does this not accepting to use the values of selectInput within Server function ?

library(shiny)
library(tidyverse)

mod_ui <- function(id){
  ns <- shiny::NS(id)
  shiny::tagList(
  selectInput('dataset', 'Choose Dataset', c('mtcars', 'iris')),
  uiOutput(ns('x')),
  uiOutput(ns('y')),
  plotOutput(ns('plot'))
  )
}

mod_server =  function(input, output, session) {

  output$x= renderUI({
    mydata = get(input$dataset)
    ### ?? Should ns() be applied here as well ? 
    selectInput(ns('columns2'), 'X axis', names(mydata))
  })
  output$y = renderUI({
    mydata = get(input$dataset)
    ### ?? Should ns() be applied here as well ?
    selectInput(ns('columns3'), 'Y axis', names(mydata))
  })

  output$plot = renderPlot({
    mydata = get(input$dataset)
    mydata <- mydata[,c(input$columns2, input$columns3)]
    plot(mydata)
  })
}

ui <- 

  shinydashboard::dashboardPage(
    skin = "yellow",
    shinydashboard::dashboardHeader(
      title = "Modularizing App"
    ),
    shinydashboard::dashboardSidebar(
      shinydashboard::sidebarMenu(id = "menu",
                                  shinydashboard::menuItem('Example', tabName = 'example')
      )
    ),
    shinydashboard::dashboardBody(
      shinydashboard::tabItems(
        shinydashboard::tabItem("example", mod_ui("ui"))
      )
    )
  )

server <- function(input, output) {
  displayFile <- shiny::callModule(mod_server, "ui")
}

shinyApp(ui,server)

If I understood you correctly, you want the plot to show up in your example. This would be one way to do it.

ui <- bootstrapPage(
    selectInput('dataset', 'Choose Dataset', c('mtcars', 'iris')),
    uiOutput('x'),
    uiOutput('y'),
    plotOutput('plot')
  )

server = function(input, output){

    output$x= renderUI({
      mydata = get(input$dataset)
      selectInput('columns2', 'X axis', names(mydata))
    })
    output$y = renderUI({
      mydata = get(input$dataset)
      selectInput('columns3', 'Y axis', names(mydata))
    })

    output$plot = renderPlot({
      mydata = get(input$dataset)
      mydata <- mydata[,c(input$columns2, input$columns3)]
      plot(mydata)
    })
}

shinyApp(ui, server)
1 Like

In order to understand the values inside input$columns2 and/or input$columns3,
added browser() but it seems to be NULL
Could you please help me to understand why I cant peep into in Browser mode ?

output$plot = renderPlot({ mydata = get(input$dataset)
browser() # In order to check values of input$columns2 or input$columns3
mydata <- mydata[,c(input$columns2, input$columns3)] plot(mydata) })

If you select the run external option it should work...at least it works for me.

Quick Q,
If this was a module,
should ns() should be within Server function also ?
Because either way, it still shows NULL
i.e

mod_ui <- bootstrapPage(
    selectInput('dataset', 'Choose Dataset', c('mtcars', 'iris')),
    uiOutput(ns('x')),
    uiOutput(ns('y')),
    plotOutput(ns('plot'))
  )

mod_server = function(input, output){

    output$x= renderUI({
      mydata = get(input$dataset)
### ?? Should ns() be applied here as well ? 
      selectInput(ns('columns2'), 'X axis', names(mydata))
    })
    output$y = renderUI({
      mydata = get(input$dataset)
### ?? Should ns() be applied here as well ?
      selectInput(ns('columns3'), 'Y axis', names(mydata))
    })

    output$plot = renderPlot({
      mydata = get(input$dataset)
      mydata <- mydata[,c(input$columns2, input$columns3)]
      plot(mydata)
    })
}

#### Call via server and UI of the main file

please find the reprex and help me

library(shiny)
library(tidyverse)

mod_ui <- function(id){
  ns <- shiny::NS(id)
  shiny::tagList(
  selectInput('dataset', 'Choose Dataset', c('mtcars', 'iris')),
  uiOutput(ns('x')),
  uiOutput(ns('y')),
  plotOutput(ns('plot'))
  )
}

mod_server =  function(input, output, session, file) {
  
  output$x= renderUI({
    mydata = get(input$dataset)
    ### ?? Should ns() be applied here as well ? 
    selectInput(ns('columns2'), 'X axis', names(mydata))
  })
  output$y = renderUI({
    mydata = get(input$dataset)
    ### ?? Should ns() be applied here as well ?
    selectInput(ns('columns3'), 'Y axis', names(mydata))
  })
  
  output$plot = renderPlot({
    mydata = get(input$dataset)
    mydata <- mydata[,c(input$columns2, input$columns3)]
    plot(mydata)
  })
}


ui <- 
  
  shinydashboard::dashboardPage(
    skin = "yellow",
    shinydashboard::dashboardHeader(
      title = "Modularizing App"
    ),
    shinydashboard::dashboardSidebar(
      shinydashboard::sidebarMenu(id = "menu",
                                  shinydashboard::menuItem('Example', tabName = 'example')
      )
    ),
    shinydashboard::dashboardBody(
      shinydashboard::tabItems(
        shinydashboard::tabItem("example", mod_ui("ui"))
      )
    )
  )

server <- function(input, output) {
  displayFile <- shiny::callModule(mod_server, "ui")
}

shinyApp(ui,server)

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