Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
541 views
in Technique[技术] by (71.8m points)

r - Passing data within Shiny Modules from Module 1 to Module 2

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...