How do I change input widgets in a Shiny module

When using shiny modules, is there a way to change certain input widgets inside a shiny module. Here is some reprex;

I have the following module to create a header section on a shiny page. You'll notice that there are 2 action buttons;


nameUI <- function(id) {
  ns <- NS(id)
  tagList(
      wellPanel(
          id = ns("ui_input_well"),
          div(
              # first action button
              actionButton(ns("add_qty"), "Add Stock Qty", icon = icon("plus")),
              div(
                  class = "pull-right",
                  # second action button
                  actionButton(ns("info_button"), "Help", icon = icon("info-circle"))
              )
          )
      )
  )
}

nameServer <- function(id) {
  moduleServer(
    id,
    function(input, output, session) {
      
    }
  )
}


So I'm able to use this module on a shiny tab. See example below -

library(shiny)
source("reprex_functions.R")

ui <- fluidPage(
    titlePanel("Simple Shiny App"),
    mainPanel(
        h1("Hello, Shiny!"),
        
        hr(),
        
        # using the module
        nameUI("header")
        
        
        
    )
)

server <- function(input, output) {
    # This is an empty server function for this example
}

shinyApp(ui, server)



Now say I wanted to use the same module on another tab, but instead of the first action button, I wanted to add numericInput("num", label = h3("Numeric input"), value = 1) instead. Is this possible to do in a module, or do I need to create a new module since I'm changing an input widget.

nameUI <- function(id, option = 1) {
  ns <- NS(id)

  more <- div()
  if (isTruthy(option == 1)) {
    more <- actionButton(ns("add_qty"),
      "Add Stock Qty",
      icon = icon("plus")
    )
  } else if (isTruthy(option == 2)) {
    more <- numericInput(ns("num"),
      label = h3("Numeric input"), value = 1
    )
  }
  tagList(
    wellPanel(
      id = ns("ui_input_well"),
      div(
        # first action button
        more,
        div(
          class = "pull-right",
          # second action button
          actionButton(ns("info_button"), "Help", icon = icon("info-circle"))
        )
      )
    )
  )
}

nameServer <- function(id) {
  moduleServer(
    id,
    function(input, output, session) {

    }
  )
}

library(shiny)

ui <- fluidPage(
  titlePanel("Simple Shiny App"),
  mainPanel(
    tabsetPanel(
      tabPanel("1", div(
        h1("Hello, Shiny!"),
        hr(),
        # using the module
        nameUI("header")
      )),
      tabPanel("2", div(
        h1("Hello, Shiny!"),
        hr(),
        # using the module
        nameUI("header2", option = 2)
      ))
    )
  )
)
server <- function(input, output) {
  # This is an empty server function for this example
}

shinyApp(ui, server)

@nirgrahamuk Thanks. This works great if I'm only using 2 input widgets. However for my use case, I want a lot more options lol.

You can do any number of them. And you can also produce them dynamically. Good luck

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.