Home > Back-end >  In Shiny/R how to use input from one module into other module?
In Shiny/R how to use input from one module into other module?

Time:12-08

I need to once I choose a name from selectInput this name will be used on the mod1_UI module. This is how I am doing so far:

This is my module file:

mod1_UI <- function(id) {
  ns <- NS(id)
  tagList(h1(uiOutput(ns('titulo_select'))))
}

mod1_Server <- function(id, ui) {
  moduleServer(id,
               function(input, output, session) {
                 output$titulo_select <- renderUI({
                   ui
                 })

               })
}

This is my app:

library(shiny)

ui <- fluidPage(selectInput(
  inputId = 'choices',
  label = 'Choices',
  choices = c('One Title', 'Second Title')
),

#verbatimTextOutput('verbatim'),

mod1_UI('titulo'))

server <- function(input, output, session) {
  choices <- reactive({
    input$choices
  })

 # output$verbatim <- renderText({
 #   choices()
 #})

  mod1_Server("titulo", ui = choices())

}

shinyApp(ui, server)

As you can see there is no reactivity one I change the choices options. Its only showed the first option.

I clude the verbatimOutput to show that inside the ui it is reactive, but when I consider the module its not.

CodePudding user response:

Perhaps you are looking for this.

mod1_UI <- function(id) {
  ns <- NS(id)
  tagList(
    selectInput(
      inputId = ns('choices'),
      label = 'Choices',
      choices = c('One Title', 'Second Title')
      ),
    uiOutput(ns('titulo_select'))
    )
}

mod1_Server <- function(id) {
  moduleServer(id,
               function(input, output, session) {
                 
                 choices <- reactive({
                   input$choices
                 })
                 output$titulo_select <- renderUI({
                   choices()
                 })
                 
               })
}

library(shiny)

ui <- fluidPage(
  mod1_UI('titulo')
)

server <- function(input, output, session) {
  mod1_Server("titulo")
}

shinyApp(ui, server)

CodePudding user response:

You have to pass the reactive conductor to the module, that is to say don't put the parentheses:

mod1_Server("titulo", ui = choices)

Then in the module you have to use the parentheses: ui().

  • Related