Mark
Mark

Reputation: 113

R Shiny How to access slider min & max values in server code

Is there a way to reference the minimum & maximum values of a slider within the server.R portion of a Shiny app?

For instance, given the following definition within ui.R:

sliderInput("slider1", "", min = 0, max = 100, value = 50)

how do I determine that the preset minimum is 0, and the maximum is 100?

Thank you.

Upvotes: 4

Views: 1422

Answers (2)

Mark
Mark

Reputation: 113

I've now found that one can have a third file as part of a shiny app, called global.R.

So, for the use case above I could define the min and max parameters as constants in the global.R file or dynamically create the slider as per Paul's suggestion.

Upvotes: 0

Paul de Barros
Paul de Barros

Reputation: 1200

It depends on what you want to do with the min and max values when you access them. I assume that you don't want to just read them, since you would already know their fixed values. If you want to be able to manipulate them, you could try defining the slider in server.R using renderUI. Then you could set the min and max parameters to variables, which could be manipulated elsewhere. Below is an example of that.

ui.R

library(shiny)
shinyUI(fluidPage(
  titlePanel("Access the min and max of a slider"),
  sidebarLayout(
    sidebarPanel(uiOutput("SliderWidget")),
    mainPanel()
  )
))

server.R

library(shiny)
shinyServer(function(input, output) {
  SlideMax = 100
  SlideMin = 0
  output$SliderWidget <- renderUI({
    sliderInput("Slider1","",min = SlideMin,max = SlideMax,value = 50)
  })
})

Of course, your question actually asks how to determine the preset values. I'm not sure I understand that. I think that the preset values would be static, so there would be no need to determine them.

Upvotes: 1

Related Questions