R Shiny Sliderinput with Restricted Range

R shiny sliderInput with restricted range

Building up from your previous question you can use the min values, here slider 1 is restricted to 80 max and slider 2 restricted to 50

rm(list = ls())
library(shiny)

slider1limit <- 80
slider2limit <- 50

ui <-pageWithSidebar(

# Application title
headerPanel("Sliders should sum to 100!"),
# Sidebar with sliders whos sum should be constrained to be 100
sidebarPanel(
sliderInput("slider1", "Slider 1: ", min = 0, max = 100, value = 0, step=1),
uiOutput("slider")),

# Create table output
mainPanel(tableOutput("restable"))
)

server <- function(input, output,session) {

observeEvent(input$slider2,{
values <- min((100 - input$slider2),slider1limit)
updateSliderInput(session, "slider1", min =0,max=100, value = values)
})
output$slider <- renderUI({
values <- min((100 - input$slider1),slider2limit)
sliderInput("slider2", "Slider 2: ", min=0,max=100, value = values)
})

output$restable <- renderTable({
myvals<- c(input$slider1, input$slider2, 100-input$slider1-input$slider2)
data.frame(Names=c("Slider 1", "Slider 2", "Slider 3"),Values=myvals)
})
}
runApp(list(ui = ui, server = server))

Restrict SliderInput in R Shiny date range to weekdays

I was facing the same problem and this worked for me. The solution relies on shinyWidgets::sliderTextInput(). For this to work you must define your choices of dates and default values up front (I'd recommend somewhere before the UI):

#packages
library(shinyWidgets)
library(lubridate)

#date choices
choices <- seq.Date(date("2021-01-01", today() - 1, by = 1)
choices <- choices[!wday(choices) %in% c(1, 7)] #removes weekends
default <- seq.Date(today() - 182, today() - 180, by = 1)
default <- default[!wday(default) %in% c(1, 7)]
default <- max(default) #most recent weekday

Then you want to stick this within the appropriate place in your UI in place of sliderInput():

sliderTextInput("trajectory", "Date Range:", choices = choices,
selected = c(default, max(choices)))

You'll retain many of the benefits of sliderInput doing it this way, but you may have to work with the grid option to get tick marks and labels to your liking.

r shiny sliderInput with exact values instead of evenly divided range

I think the sliderTextInput from shinyWidgets does what you want. Though on the slider, all values are equally separated and not proportionnally.

library(shiny)
library(shinyWidgets)

ui <- fluidPage(

sliderTextInput(
inputId = "myslider",
label = "Choose a value:",
choices = c(2,3,5,7,11,13,17,19,23,29,31),
grid = TRUE
)
)

server <- function(input, output, session) {
observe(print(input$myslider))
}

shinyApp(ui, server)

Defining a specific range of values for a sliderInput in Shiny

You can call updateSliderInput every time the slider is changed, to enforce the interval.

The trick here is to remember previous value to update the right handler (the one that that didn't change)

INTERVAL = 13
value = c(2, 2 + INTERVAL)

ui <-basicPage(
sliderInput("id", "Ranking",
min = 2, max = 60, value = value))

server <- server <- function(input, output, session) {
observeEvent(input$id,{
newvalue = input$id

if(value[1] != newvalue[1] && newvalue[2] - newvalue[1] != INTERVAL)
updateSliderInput(session, "id", value = c(newvalue[1], newvalue[1] + INTERVAL))

if(value[2] != newvalue[2] && newvalue[2] - newvalue[1] != INTERVAL)
updateSliderInput(session, "id", value = c(newvalue[2] - INTERVAL, newvalue[2]))

value <<- newvalue
})
}

shinyApp(ui, server)

Is it possible to restrict Rhiny SliderInput's intervals while traversing a wider interval?

Nevermind, I had a fellow co-worker answered my question. Apparently you need to subtract an hour from the maximum

                     max = as.POSIXct("2020-12-31 23:59:59") - hours(1),
value = as.POSIXct("2020-12-15 17:00:00"),
timeFormat = "%Y-%m-%d %H:%M", ticks = T, step=3600, animate = T

shiny: is it possible to combine updateSliderInput() with a restricted sliding range based on radioButton()?

You can update the slider again, like so:

library(shiny)
library(shinyjs)

sliderInput2 <- function(inputId, label, min, max, value, step=NULL, from_min, from_max){
x <- sliderInput(inputId, label, min, max, value, step)
x$children[[2]]$attribs <- c(x$children[[2]]$attribs,
"data-from-min" = from_min,
"data-from-max" = from_max,
"data-from-shadow" = TRUE)
x
}

ui <- fluidPage(
useShinyjs(),
radioButtons("EXBR", "External Beam Radiation", choiceValues=list("No","Yes"),
choiceNames=list("No","Yes"), selected ="No", inline=T),
sliderInput2("EXBRGy", "Cumulative Gy",
min = 0, max = 60, value = 54.2, step = 0.2, from_min = 40, from_max = 60
)
)

server <- function(input, output, session) {

rvs <- reactiveValues(prev_value = 54.2)

observeEvent(input$EXBR, {
if(input$EXBR == "No"){
updateSliderInput(session, "EXBRGy",min = 0, max = 0, value=0)
rvs$prev_value <- input$EXBRGy
disable("EXBRGy")
}else{
updateSliderInput(session, "EXBRGy", min = 0, max = 60, value = rvs$prev_value)
enable("EXBRGy")
}
})

observeEvent(input$EXBRGy, {
print(input$EXBRGy)
})
}

shinyApp(ui, server)

Shiny slider works for numeric ranges, but not for dates

Your code is working fine:

library(shiny)

ui <- fluidPage(sliderInput(
inputId = "range",
label = "Range",
min = as.Date("2021-01-01"),
max = as.Date("2021-12-31"),
value = c(as.Date("2021-02-02"), as.Date("2021-03-03"))
))

server <- function(input, output, session) {}

shinyApp(ui, server)

result



Related Topics



Leave a reply



Submit