Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 939
shamary Avatar asked Oct 04 '17 00:10

shamary


People also ask

How do modules interact with each other?

Communication between modules is done via the exchange of named objects with the event and object manager. Modules therefore do not depend on specific other modules being available in memory or having been run before, but only on whether the necessary data objects have been stored in the event or the object manager.

What is ns in Shiny?

The NS function creates namespaced IDs out of bare IDs, by joining them using ns. sep as the delimiter. It is intended for use in Shiny modules. See http://shiny.rstudio.com/articles/modules.html.

Which of the following is used to interact between modules?

Shiny - Communication between modules.

Why use Shiny modules?

Shiny modules have two big advantages. Firstly, namespacing makes it easier to understand how your app works because you can write, analyse, and test individual components in isolation. Secondly, because modules are functions they help you reuse code; anything you can do with a function, you can do with a module.


1 Answers

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)
like image 181
Gregor de Cillia Avatar answered Sep 21 '22 20:09

Gregor de Cillia