'How to compute value for InputSlider R Shiny?
I'm doing a Shiny app and I have a question. I want to have a slider input which min and max values depends on values in some dataframe. That's easy to make. But I want the value of slider input to be mean of those min and max values in input and don't know how to data. I don't know if anyone will understand but maybe code will help.
ui.R
sliderInput("param1", "Param1", min = 0, max = 1, value = 0, step = 0.01),
server.R
observeEvent( updateSliderInput(inputId = "param1", min = ... //getting the value from df,
max = ... //getting the value from df,
value = input$param1$min + (input$param1$max - input$param1$min/2)) //that's what I want to do but it's not working
Hope someone can help. Thanks!
Solution 1:[1]
As you haven't provided any reproducible demo and error information, I can only tell you what I found wrong.
The value argument in updateSliderInput() need to be The initial value of the slider, either a number, a date (class Date), or a date-time (class POSIXt). A length one vector will create a regular slider; a length two vector will create a double-ended range slider. Must lie between min and max..
In your case, the value is equal to input$param1$min/2 + input$param1$max, which is greater than input$param1$max.
Update
Based on the code you provided, I would like to mention several things.
shinyUI()andshinyServer()are superseded in the current version of Shiny. Although it doesn't generate any error, I would suggest you to not use them.To use
updateSliderInput(), you need input updater functions send a message to the client. Therefore, you should combineupdateSliderInput()with some functions likeobserve()orobserveEvent().The value generated from the double-ended range slider is a vector of length two. You should use
input$param1[1]instead ofinput$param1$min.There's some reactivity problem in your code. I would suggest you to understand how shiny update the value. This link may be useful for this part.
I'm not very sure about what you want, but I would put what I understand you want below.
ui = fluidPage(
navbarPage(title = "Title",
tabPanel("Section1",
div("text"),
),
tabPanel(
"Section2",
sidebarPanel(
sliderInput(inputId = "param1",
label = "SlideBar",
min = min(iris$Sepal.Length),
max = max(iris$Sepal.Length),
value = min(iris$Sepal.Length)/2 + max(iris$Sepal.Length)/2),
actionButton("submit", "Submit")
),
mainPanel(h1('Text'))
)
)
)
server = function(input, output) {
observe({
print(input$param1)
})
}
shinyApp(ui = ui, server = server)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
