R Shiny input slider range values - r

I have a slider input type of 'range' in my Shiny app and I would like to get the minimum and maximum values selected by the user in my server.R
in my ui.R I have
sliderInput("range", "Age:",
min = 0, max = 100, value = c(0,100))
and I would like to get the selected values not min and max that I have defined

You can access the slider ranges using input$range[1] for min and input$range[2] for max
library(shiny)
ui <- basicPage(sliderInput("range", "Age:",min = 0, max = 100, value = c(0,100)),textOutput("SliderText"))
server <- shinyServer(function(input, output, session){
my_range <- reactive({
cbind(input$range[1],input$range[2])
})
output$SliderText <- renderText({my_range()})
})
shinyApp(ui = ui, server = server)

Related

Get a linear date Shiny slider input to "stick" on only specified irregular dates

I am using a Shiny date slider but there are only certain dates with observations. When the user moves the slider I would like it to jump to the nearest observation date when released. I could use an index value to step through each observation but I want to preserve the spatial appearance of a linear timeline.
My hunch is to render the slider on the fly, updating the value parameter with the nearest available observation date whenever the inputId of the slider changes, but this creates a circular argument with inputId and value parameters.
The example below shows my attempt to avoid this circularity. Note that if you comment out value = ifelse(length(nearest_date()) > 0,nearest_date(),min(date_vec)), and replace it with value = min(date_vec), the code runs fine (just not what I want).
library(shiny)
#> Warning: package 'shiny' was built under R version 4.1.3
date_vec <- as.Date("2022-01-01") +
cumsum(round(runif(10, 1, 20)))
ui <- fluidPage(uiOutput("jumpySlider"),
mainPanel(textOutput("this_date"),
textOutput("desired_date")))
server <- function(input, output) {
nearest_date <- reactive({
date_vec[which.min(abs(as.numeric(input$date) - as.numeric(date_vec)))]
})
output$jumpySlider <- renderUI({
sliderInput(
"date",
"Date",
min = min(date_vec),
max = max(date_vec),
# value = min(date_vec),
# PROBLEM LINE
value = ifelse(length(nearest_date()) > 0,nearest_date(),min(date_vec)),
animate = animationOptions(interval = 100)
)
})
output$this_date <- renderText({
paste("Slider Date:", format.Date(input$date))
})
output$desired_date <- renderText({
paste("Desired Date:", format.Date(nearest_date()))
})
}
shinyApp(ui = ui, server = server)
Shiny applications not supported in static R Markdown documents
Created on 2022-07-23 by the reprex package (v2.0.1)
I am using observe to update the sliderInput to the closest date. The sliderInput "jumps" to the closest date after the input from the user.
library(shiny)
set.seed(2022)
date_vec <- as.Date("2022-01-01") + cumsum(round(runif(10, 1, 20)))
ui <- fluidPage(
sliderInput(
"date",
"Date",
min = min(date_vec),
max = max(date_vec),
value = min(date_vec)
),
mainPanel(
textOutput("this_date")
)
)
server <- function(input, output, session) {
observe({
req(input$date)
updateSliderInput(session, "date",
value = date_vec[which.min(abs(date_vec - input$date))])
})
output$this_date <- renderText({
paste("Slider Date:", format.Date(input$date))
})
}
shinyApp(ui = ui, server = server)

How to adjust numericinput in a app shiny

I have an app with two numericinput. Both presented values ​​between 0 and 1. What I wanted to do is the following: As the sum of the two weights must equal 1, so when I select the first weight, for example, 0.2, the second will be 0.8. Got the idea?
Executable code below
library(shiny)
ui <- fluidPage(
numericInput("weight1", label = h4("Weight 1"),
min = 0, max = 1, value = 0.5),
numericInput("weight2", label = h4("Weight 2"),
min = 0, max = 1, value = 0.5),
helpText("The sum of weights should be equal to 1"),
hr(),
fluidRow(column(3, verbatimTextOutput("value1"))),
fluidRow(column(3, verbatimTextOutput("value2")))
)
server <- function(input, output,session) {
output$value1 <- renderPrint({ input$weight1 })
output$value2 <- renderPrint({ input$weight2 })
}
shinyApp(ui = ui, server = server)
You can do it by using observeEvent and updateNumericInput.
Here's what the code will look like:
server <- function(input, output,session) {
observeEvent(input$weight1, {
updateNumericInput(session, 'weight2',
value = 1 - input$weight1)
})
output$value1 <- renderPrint({ input$weight1 })
output$value2 <- renderPrint({ input$weight2 })
}
Note: You don't need updateNumericInput if you are dealing with only two numbers and every time you need the sum to be equal to 1.

R Shiny and reactive value in shapiro.test()

I want to perform the Shapiro-Wilk-Test in my Shiny App. Over the two sliders and the numeric input I get the values for rnorm. And because I can change my inputs to rnorm, I made it reactive.
But when running the app it doesn't work - I tried a lot, but it wasn't successful.
How must I change my code that it works? Thanks in advance!
My code:
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
sliderInput("slider1", label = h3("Quantity of n"), min = 10, max = 100, value = 50),
sliderInput("slider2", label = h3("Mean of group 1"), min = 0, max = 100, value = 50),
numericInput("num", label = h3("SD of group 1"), value = 1)
),
mainPanel(
textOutput("Text1")
)
)
)
server <- function(input, output) {
group1=reactive({
rnorm(n=input$slider1,mean=input$slider2,sd=input$num)
})
# Shapiro-Wilk-Test
### shapiro.test(....)[[2]][1] shows the p-value
ND_G1=shapiro.test(group1())[[2]][1] # with "rnorm(10,5,2)" instead of group1() it works
output$Text1 <- renderText({paste("The p-value is: ",ND_G1)})
}
# Create Shiny app ----
shinyApp(ui = ui, server = server)

Is there a way to set the max value of a numericInput() as the value of another numericInput()?

I have a shiny app with two numericInput(). One for the minimum value and the second for the maximum value. Is it possible to set the max value of the minimum as the value of the maximum?
library(shiny)
ui <- fluidPage(
uiOutput("filter")
)
server <- function(input, output) {
output$filter<-renderUI({
"range"=tagList(
numericInput("obs", "Min:", 10, min = 1, max = 100),
numericInput("obs2", "Max:", 10, min = 1, max = 100)
)
})
}
shinyApp(ui, server)
You can use renderUI():
library(shiny)
ui <- fluidPage(
uiOutput("obs1"),
numericInput("obs2", "Max:", 10, min = 1, max = 100),
verbatimTextOutput("value")
)
server <- function(input, output) {
output$value <- renderText({
input$obs
})
output$obs1 <- renderUI({
val <- min(input$obs, as.numeric(input$obs2))
numericInput("obs", "Min:", val, min = 1, max = as.numeric(input$obs2))
})
}
shinyApp(ui, server)

Set maximum sliderInput value based on reactive output value

I have a Shiny app with a slider input and I would like to set the maximum possible value for the slider based on a maximum value in the dataset uploaded by the user. The max distance will change based on the dataset uploaded.
Here is a minimum working example of what I am trying to do. Below I just hardcode the number for the maxdistance, but in my code it is calculated:
library(shiny)
ui <- fluidPage(
sliderInput("range_one", "Core:", min = 0, max = textOutput("maxdistance"), value = c(0,0))
)
server <- function(input,output) {
output$maxdistance <- renderText({
maxdistance <- 250
return(maxdistance)
})
}
shinyApp(ui=ui,server=server)
I get the following error:
Error in max - min : non-numeric argument to binary operator
Which makes sense because I as asking for a text output, so how do I get this output as a numeric value for use in the sliderInput() function?
Here is an example.
library(shiny)
ui <- shinyUI(fluidPage(
titlePanel("Example"),
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30),
actionButton("change", "Change slider max value")
),
mainPanel(
plotOutput("distPlot")
)
)
))
server <- shinyServer(function(input, output, session) {
observeEvent(input$change, {
max = sample(50:100, 1)
updateSliderInput(session, "bins", max=max)
})
output$distPlot <- renderPlot({
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
})
shinyApp(ui = ui, server = server)
Change as follows, it will work:
sliderInput("range_one", "Core:",
min = 0, max = as.integer(textOutput("maxdistance")),
value = c(0,0))
Here is the code I am using on the server side to achieve the desired result of my original question, without the need for an action button:
observe({
infile <- input$file # user input file upload
if(!is.null(infile)) {
processed <- processed() # list of processed elements returned from earlier reactive({}) function with my app
processed_data <- processed$processed_data # get the processed data from the list and save as data frame
maxdistance <- max(processed_data$distance) # calculate the max distance from the processed data
updateSliderInput(session, "range_one", max=maxdistance) # update the slider called "range_one" based on the maxdistance
}
})
This allows the app to use the default maximum slider value until a file is uploaded. Once the user uploads a file, the data is processed and the slider is updated.

Resources