In my code I use two selectInput()
functions: one for the indicating the start period to feed into a custom function, and another for indicating the end period to feed into the same custom function. At the bottom is a simplified MWE extract of the code, which doesn't use this custom function, using instead rbind()
to join and output the to/from data for sake of simplicity. In the full code, the end period must always be greater than (>) the start period for the custom function to work.
How would I make the choices in the "To" (selectInput(inputId = "toPeriod"...
) reflect only those values > than what was input by the user in the "From" (selectInput(inputId = "fromPeriod"
) function?
I realize this requires making the to/from input choices reactive, so I started by moving the selectInput()
functions into the server
section using renderUI
, but I stopped when receiving the message "Warning: Error in : Problem with filter()
input ..1
." even though the output is correct. In any case, both before and after moving the selectInput()
functions into the server
section, this code seems to run slowly.
This image better explains:
There are other posts getting at the same issue but either the code examples are overly-cumbersome or the questions/answers are poorly written or explained: R Shiny selectInput Reactivity, R shiny passing reactive to selectInput choices, Vary the choices in selectinput based on other conditions in shiny R, etc.
MWE code:
library(dplyr)
library(shiny)
library(tidyverse)
data <-
data.frame(
ID = c(1,1,1,2,2,2,3,3,3),
Period = c(1, 2, 3, 1, 2, 3, 1, 2, 3),
Values = c(5, 10, 15, 0, 2, 4, 3, 6, 9)
)
ui <- fluidPage(
tableOutput("data"),
uiOutput("fromPeriod"),
uiOutput("toPeriod"),
tableOutput("dataSelect")
)
server <- function(input, output) {
output$fromPeriod <- renderUI({
selectInput(inputId = "fromPeriod",label = "From period:",choices = unique(data$Period), selected = 1)
})
output$toPeriod <- renderUI({
selectInput(inputId = "toPeriod",label = "To period:",choices = unique(data$Period), selected = 2)
})
output$data <- renderTable({data})
output$dataSelect <- renderTable({
part1 <- data %>% filter(Period == input$fromPeriod)
part2 <- data %>% filter(Period == input$toPeriod)
rbind(part1,part2)
}, rownames = TRUE)
}
shinyApp(ui, server)