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


i'd know how access reactive functions in 1 module module. if have more 1 reactive function in module can access them each in module?

so how call/pass reactive function in 1 shiny module shiny module (reactive function 1 module 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 want dataone above module1        # tried use callmodule(module1,_) didnt understand id in case?      }      library(shiny)      ui <- fluidpage(  sliderinput("slider","how 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 test if able correctly dataone()      #from module2       output$datap<-renderprint(         lapply(1:input$slider, function(i){ datas<-callmodule(module2,i)           datas()})      )       }      shinyapp(ui, server) 

thanks

you need use return in module return reactive app or module calls nested module. in calling layer, use <- assign returned value. usable in layer calls nested module. see 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 visible in app   return(dataone)  } module2<-function(input, output, session, dataone){   # if want use dataone() here, need in reactive context, e.g. observe, render*, ...   observeevent(dataone(), { print(dataone()) })    # return dataone visible in app   return(dataone) }  library(shiny)  ui <- fluidpage(  sliderinput("slider","how cars?", 1, 10, 1, width         = "100%" ),                   uioutput("selectors"),                   verbatimtextoutput("datap") )  server <- function(input, output, session){    # create list dataone-vectors   dataones <- list()   for(i in 1:10)     # fill list each dataone     dataones[[i]] <- callmodule(module1, i)    output$selectors <- renderui({     lapply(1:input$slider, module1ui)   })    #below  code test if able correctly 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) 

enter image description here


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -