Passing data within Shiny Modules from Module 1 to Module 2

杀马特。学长 韩版系。学妹 提交于 2019-12-18 04:42:19

问题


I dont have a reproducible example as the question is more on how modules work. I am trying to understand how to pass some reactive function from one module to the next. I have received replies in the past about using ObserveEvent but they have not seem to work when I am using the reactive value in one module to perform some other operation in another module

module1 <- function(input, output, session){

data1<-reactive({
  #some reacttive funcion that produces an output

})
data2<-reactive({
  #some reacttive funcion that produces another output

})  



return(list(data1,data2))


  }

module2 <- function(input, output, session,data1){

observe( data1(), {

  #perform some other functions here using data1().e.g reading or parsing data
})


}

So basically I have a module1 that returns two outputs from data1 and data2

I want to use the value of data1 in module 2 and perform some new operation using that value.

I have looked at other answers to similar questions here but I still dont understand them. If someone can help me explain this concept more clearly that would be of great help thanks for your help


回答1:


One possibility is passing the output from one module to the other at construction time. This allows hierachic relationships between modules. There is also the possibility to create memory that is shared between two modules which I will not cover in this answer.

reactiveValues

Here i created an inputModule and an outputModule. The inputModule recieves two textinputs by the user and the output module displays them via verbatimTextOutput. The inputModule passes the user submitted data to the output module as a reactiveValues object called ImProxy (input module proxy). The outputModule accesses the data just like a list (ImProxy$text1, ImProxy$text2).

library(shiny)

inputModuleUI <- function(id){
  ns <- NS(id)
  wellPanel(h3("Input Module"),
            textInput(ns('text1'), "First text"),
            textInput(ns('text2'), "Second text"))
}
inputModule <- function(input, output, session){
  vals <- reactiveValues()
  observe({vals$text1 <- input$text1})
  observe({vals$text2 <- input$text2})
  return(vals)
}

outputModuleUI <- function(id){
  wellPanel(h3("Output Module"),
            verbatimTextOutput(NS(id, "txt")))
}
outputModule <- function(input, output, session, ImProxy){
  output$txt <- renderPrint({
    paste(ImProxy$text1, "&", ImProxy$text2)
  })
}

ui <- fluidPage(
  inputModuleUI('IM'),
  outputModuleUI('OM')
)   
server <- function(input, output, session){
  MyImProxy <- callModule(inputModule, 'IM')
  callModule(outputModule, 'OM', MyImProxy)
}

shinyApp(ui, server)

This approach can be used with observe or observeEvent as well.

list(reactive)

If you want to use reactive rather than reactiveValues, the following adaptiation of the above code can be used. You can leave the ui functions as they are.

inputModule <- function(input, output, session){
  list(
    text1 = reactive({input$text1}),
    text2 = reactive({input$text2})
  )
}

outputModule <- function(input, output, session, ImProxy){
  output$txt <- renderPrint({
    paste(ImProxy$text1(), "&", ImProxy$text2())
  })
}

shinyApp(ui, server)

reactive(list)

Again, this will give the same functionality for the app but the proxy pattern is slightly different.

inputModule <- function(input, output, session){
  reactive(
    list(
      text1 = input$text1,
      text2 = input$text2
    )
  )
}

outputModule <- function(input, output, session, ImProxy){
  output$txt <- renderPrint({
    paste(ImProxy()$text1, "&", ImProxy()$text2)
  })
}

shinyApp(ui, server)


来源:https://stackoverflow.com/questions/46555355/passing-data-within-shiny-modules-from-module-1-to-module-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!