0

I am facing a issue to access my shiny module's dynamic input value from slider in the main shiny server function.

In the following code from SelectInput when metric2 is selected, I should be able to see a slider with a specific range and a default. That works perfectly. But the value from the slider is expected to be shown as TextOutput in the main shiny server function, which is failing.

main shiny file:

library(shiny)


source("modules/showLowHighSliders.R")

ui <- fluidPage(
  fluidRow( 
    column(
      3,
      selectizeInput(
        inputId = "metricID",
        selected = NULL,
        multiple = TRUE,
        label = "Select a metric",
        choices = list(
          Type1 = c("metric1", "metric2", "metric3"),
          Type2 = c("metric4","metric5")
        ),
        options = list('plugins' = list('remove_button'))
      )
    ),
    column(2, 
           uiOutput(outputId = "lowerThresholdText"),
           showLowHighSlidersUI("sliders-metric2")
    )
  )
)

server <- function(input, output){

  
  ret <- callModule(module = showLowHighSliders, 
             id = "sliders-metric2", 
             metrics_selected=reactive(input$metricID))

  output$lowerThresholdText <- renderText({
    if(!is.null(input$metricID )){
      if(input$metricID == 'metric2'){
        paste("Lower Value: ", ret() )
        
      }
    }

  })
}

shinyApp(ui, server)

Shiny module: showLowHighSliders.R


showLowHighSlidersUI <- function(id) {
  ns <- NS(id)
  fluidPage(
    fluidRow(
      column(12, 
             tagList(
                # uiOutput(ns("lowerThresholdText")),
               uiOutput(ns("lowerThresholdSlider"))
             )
      )
    )
  )
}

# Function for module server logic
showLowHighSliders <- function(input, output, session, metrics_selected) {
  
 reactive({input$mySlider})
  
  output$lowerThresholdSlider <- renderUI({
    #print(metrics_selected())
    if(!is.null(metrics_selected() ) ){
      if('metric2' %in% metrics_selected() ){
        sliderInput(
          inputId = "mySlider",
          label = "",
          min = 0,
          max = 200,
          value = 20
        )
      }
    }
  })
  
  # output$lowerThresholdText <- renderText({
  #   #print(metrics_selected)
  #   if(!is.null(metrics_selected() )){
  #     if('SMA' %in% metrics_selected()){
  #       paste("Lower SMA: ", input$mySlider )
  #     }
  #   }
  #   
  # })
  
}

I was also unable to access the dynamic input slider value within the module itself shown in the commented part.

enter image description here

Any help is appreciated.

user2359902
  • 121
  • 1
  • 7

1 Answers1

0

Try this

# Function for module server logic
showLowHighSliders <- function(input, output, session, metrics_selected) {
  
  # reactive({input$mySlider})
  ns <- session$ns
  output$lowerThresholdSlider <- renderUI({
    #print(metrics_selected())
    if(!is.null(metrics_selected() ) ){
      if('metric2' %in% metrics_selected() ){
        sliderInput(
          inputId = ns("mySlider"),
          label = "",
          min = 0,
          max = 200,
          value = 20
        )
      }
    }
  })
  
  output$lowerThresholdText <- renderText({
    #print(metrics_selected)
    if(!is.null(metrics_selected() )){
      #if('SMA' %in% metrics_selected()){
        paste("Lower SMA: ", session$input$mySlider )
      #}
    }

  })
  
}

output

YBS
  • 19,324
  • 2
  • 9
  • 27
  • Thanks @YBS for your reply. As it worked from within the module as you pointed, it also worked from the parent application when I returned it using `reactive({session$input$mySlider})` and then catching it in the parent application as shown in the main question. Hope this is the correct way of communication between module and app. Thanks again for your reply! – user2359902 Jan 23 '21 at 15:58