简体   繁体   中英

how to call a shiny module in another shiny module (reactive function from one module to the other)

I'd like to know how to access reactive functions in one module to another module. If I have more than one reactive function in a module can I access them each in another module?

So basically how would I call/pass a reactive function in one shiny module to another shiny module (reactive function from one module to the other) Thanks

module1Ui <- function(id) {
  ns <- NS(id)

  wellPanel(selectInput(
    ns("cars"),
    "cars:",
    list(
      "Select" = "",
      "a" = "mazda",
      "b" = "ford"
    )
  ))
}


module1 <- function(input, output, session) {
  dataone <- reactive({
    if (input$cars == "mazda") {
      mtcars$mpg
    }
    else {
      (mtcars$hp)
    }
  })
}
module2 <- function(input, output, session) {
  dataone()
  # here I want dataone from the above module1
  # I tried to use callmodule(module1,_) but then didnt understand what the id would be in this case?
}

library(shiny)

ui <-
  fluidPage(
    sliderInput("slider", "how much cars?", 1, 10, 1, width = "100%"),
    uiOutput("selectors"),
    verbatimTextOutput("datap")
  )

server <- function(input, output, session) {
  for (i in 1:10)
    callModule(module1, i)
  output$selectors <- renderUI({
    lapply(1:input$slider, module1Ui)
  })

  # below  code just to test if I was able to correctly get dataone()      #from module2
  output$datap <- renderPrint(lapply(1:input$slider, function(i) {
    datas <- callModule(module2, i)
    datas()
  }))
}

shinyApp(ui, server)

Thanks

You need to use return in the module to return the reactive to the app or the module which calls your nested module. In the calling layer, use <- to assign the returned value. Then it is usable in the layer that calls your nested module. See the working example:

module1Ui <- function(id) {
  ns <- NS(id)

  wellPanel(selectInput(ns("cars"), "Select", c("mpg", "hp")))
}


module1 <- function(input, output, session) {
  dataone <- reactive({
    req(!is.null(input$cars))
    return(input$cars)
  })

  # return dataone so that it is visible in the app
  return(dataone)
}
module2 <- function(input, output, session, dataone) {
  # if you want to use dataone() here, you need to do so in a reactive context, e.g. observe, render*, ...
  observeEvent(dataone(), {
    print(dataone())
  })

  # return dataone so that it is visible in the app
  return(dataone)
}

library(shiny)

ui <-
  fluidPage(
    sliderInput("slider", "how much cars?", 1, 10, 1, width = "100%"),
    uiOutput("selectors"),
    verbatimTextOutput("datap")
  )

server <- function(input, output, session) {
  # create a list for all dataone-Vectors
  dataones <- list()
  for (i in 1:10)
    # fill the list with each dataone
    dataones[[i]] <- callModule(module1, i)

  output$selectors <- renderUI({
    lapply(1:input$slider, module1Ui)
  })

  # below  code just to test if I was able to correctly get dataone()
  #from module2
  output$datap <- renderPrint({
    lapply(1:input$slider, function(i) {
      datas <- callModule(module2, i, dataones[[i]])
      return(paste("Input", i, "returned", datas()))
    })
  })
}

shinyApp(ui, server)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM