1

I have a simple shiny module where I want to get the sum from two slider inputs:

The module codes are:

Module

custSliderGroupInput <- function(id,slider1Name,slider2Name){
    ns <- NS(id)
    tagList(sliderInput(ns("slider1"),slider1Name,1,100,50),
            sliderInput(ns("slider2"),slider2Name,1,20,10))
}

custSliderGroup <- function(input,output,session){
    rv <- reactiveVal()
    observeEvent(c(input$slider1,input$slider2),{
        rv <- reactive({input$slider1 + input$slider2})
        print(rv())
        return(list(result = rv()))
    })
}

In my app.R, I want to display the result on using textOutput, but it doesn't work and no error is displayed. (the value does get printed in the console though.)

App

library(shiny)
ui <- fluidPage(
        custSliderGroupInput("myslider","A","B"),
        textOutput("text")
    )


server <- function(input, output,session){
    output$text <- renderText({
        callModule(custSliderGroup,"myslider")$result
    })
}

shinyApp(ui = ui, server = server)

I searched on Google and StackOverflow, but all solutions just don't work.

The message does get printed in the console:
The message does get printed in the console

But nothing displayed on UI:
enter image description here

yusuzech
  • 5,896
  • 1
  • 18
  • 33
  • this might help: https://stackoverflow.com/questions/48882427/how-to-store-the-returned-value-from-a-shiny-module-in-reactivevalues – Tonio Liebrand Apr 27 '19 at 08:20
  • Thank you. I tried to modify my existing coding use the template in that solution. But it still didn't work. callModule should be triggered correctly since values are printed to the console. But the problem is that the values can't be passed to textOutput. – yusuzech Apr 27 '19 at 08:49

1 Answers1

0

I somehow solved it by doing this:

Module:

custSliderGroupInput <- function(id,slider1Name,slider2Name){
    ns <- NS(id)
    tagList(sliderInput(ns("slider1"),slider1Name,1,100,50),
            sliderInput(ns("slider2"),slider2Name,1,20,10))
}

custSliderGroup <- function(input,output,session){
    rv <- input$slider1 + input$slider2
    return(rv)
}

App

ui <- fluidPage(
        custSliderGroupInput("myslider","A","B"),
        textOutput("text")
    )


server <- function(input, output,session){
    output$text <- renderText({
        callModule(custSliderGroup,"myslider")
    })
}

shinyApp(ui = ui, server = server)

I don't know why, but it seems like using functionalities such as reactive() or observeEvent() makes the module environment too complicated and does more harm than good. It just works by simplifying the codes. If anyone knows how this theoretically works or doesn't work please post your answer!

Thanks a lot!

yusuzech
  • 5,896
  • 1
  • 18
  • 33
  • I just wanted to answer you, but good you solved it. You ran into trouble because ObserveEvent exists to capture side effects. If you want to return a value, always use EventReactive. – DSGym Apr 27 '19 at 09:38
  • Thank you so much. I just tried eventReactive and it worked like a charm. – yusuzech Apr 27 '19 at 19:53
  • great, not so intuitive, i know. I also had issues with plots rendering multiple times because I used the wrong one :-) – DSGym Apr 27 '19 at 19:57