Elias
Elias

Reputation: 811

How to set an initial value for two dependent input values (Slider and Numeric) in shiny?

I have achieved to define two interconnected or mutually dependent input in my shiny app. Right now, my problem is to set a specific initial value for these slider and numeric inputs. It seems that they always start with the minimum value, even I don't now exactly why. How can I indicate a unique starting point or an initial value for these input parameters?

I have attached a simplified part of my app in order to provide you a reproducible version of my problem here:

"ui.R"

library(shiny)

shinyUI(fluidPage(

  uiOutput("Param_s"),
  uiOutput("Param_n")

))

and the "server.R"

library(shiny)

shinyServer(
function(input,output) {

# Mutually dependent  slider and numeric inputs 
output$Param_s = renderUI({
  sliderInput(inputId = "param_slide",
            label= "My input parameter",
            value= input$param_numeric,
            min=1,
            max=200)
 })

output$Param_n = renderUI({
  numericInput(inputId = "param_numeric",
             label= "My input parameter",
             value= input$param_slide,
             min=1,
             max=200)
})


})

I tried various things to fix the initial value but eventually nothing worked. Any help would be appreciated!!

Upvotes: 1

Views: 936

Answers (2)

ismirsehregal
ismirsehregal

Reputation: 33397

Coming from here.

You should try to avoid re-rendering (renderUI) if possible.

It's faster to update existing inputs:

library(shiny)

ui <- fluidPage(
  sliderInput(
    inputId = "param_slide",
    label = "My input parameter",
    value = 60,
    min = 1,
    max = 200
  ),
  numericInput(
    inputId = "param_numeric",
    label = "My input parameter",
    value = 60,
    min = 1,
    max = 200
  )
)

server <- function(input, output, session) {
  observeEvent(input$param_numeric, {
    updateSliderInput(session, "param_slide", value = input$param_numeric)
  }, ignoreInit = TRUE)
  observeEvent(input$param_slide, {
    updateNumericInput(session, "param_numeric", value = input$param_slide)
  }, ignoreInit = TRUE)
}

shinyApp(ui, server)

Furthermore, please check this and this.

Upvotes: 1

Elias
Elias

Reputation: 811

wow! I got it guys! You should only update the two input objects at the same time and up to the same value. It means adding these two lines solved my problem to set the initial value to 60 for example:

updateSliderInput(session,"param_slide", value = 60)
updateNumericInput(session,"param_numeric", value = 60 )

Therefore the whole "server.R" would be like this:

#
library(shiny)

shinyServer(
function(input,output,session) {

# Mutually dependent  slider and numeric inputs 
output$Param_s = renderUI({
sliderInput(inputId = "param_slide",
            label= "My input parameter",
            value= input$param_numeric,
            min=1,
            max=200)
})

output$Param_n = renderUI({
numericInput(inputId = "param_numeric",
             label= "My input parameter",
             value= input$param_slide,
             min=1,
             max=200)
})

updateSliderInput(session,"param_slide", value = 60)
updateNumericInput(session,"param_numeric", value = 60 )

})

You should only be aware of adding these updates with an

observeEvent()

when you have these input objects on the other tabs. In my case which I am using "sidebarMenu" I used a simple line of code as this:

observeEvent(input$sidebar_id =="tab1",{
  updateSliderInput(session,"param_slide", value = 60)
  updateNumericInput(session,"param_numeric", value = 60 )
})

Upvotes: 2

Related Questions