Hello,
I have an application that maintains three tabs. One of the tabs has 12 selectInput controls and 2 plotly plots. When the first selectInput changes, it updates the rest of the controls and generates a plot. When one or several or all of the controls change, their values are reflected on the two plots of the tab. My problem is that when an input fires, its change is reflected only in one scenario and ignores the rest. I tried to track down the bug using:
options(shiny.reactlog=TRUE)
and launched the reactive log visualization to find that each output or update scenario is triggered alone. Here is a small code example that reflects my design but does not reproduce the issue.
server.R
library(shiny)
library(plotly)
library(dplyr)
shinyServer(function(input, output,session) {
output$first <- renderPlotly({
# generate bins based on input$bins from ui.R
cars.subset <- cars[1:input$cars,] %>%
filter(speed >= input$speedRange[1] & speed <= input$speedRange[2]) %>%
filter(dist >= input$distanceRange[1] & dist <= input$distanceRange[2])
plot_ly(cars.subset, x=~speed, y=~dist, type="scatter")
})
output$second <- renderPlotly({
cars.subset <- cars[1:input$cars,] %>%
filter(speed >= input$speedRange[1] & speed <= input$speedRange[2]) %>%
filter(dist >= input$distanceRange[1] & dist <= input$distanceRange[2])
plot_ly(cars.subset,x=~dist, y=~speed, type="box")
})
observeEvent(input$cars,{
updateSliderInput(session,"display",value = input$cars)
cars.subset <- cars[1:input$cars,]
min.speed <- min(cars.subset[,1])
max.speed <- max(cars.subset[,1])
min.distance <- min(cars.subset[,2])
max.distance <- max(cars.subset[,2])
updateSliderInput(session, "speedRange", value = c(min.speed,max.speed))
updateSliderInput(session, "distanceRange", value=c(min.distance,max.distance))
})
})
ui.R
library(shiny)
library(plotly)
shinyUI(fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("cars",
"Number of cars:",
min = 1,
max = 50,
value = 25),
sliderInput("display",
"Number of cars chosen:",
min = 1,
max = 50,
value = 1),
sliderInput("speedRange",
"Speed Range",
min = 4,
max = 25,
dragRange = TRUE,
value = c(4,25)),
sliderInput("distanceRange",
"Distance Range",
min = 1,
max = 120,
dragRange = TRUE,
value = c(1,120))
),
# Show a plot of the generated distribution
mainPanel(
plotlyOutput("first"),
plotlyOutput("second")
)
)
))
Any suggestions on how to handle such problems? And how to make sure that an input change reflects in all outputs or updates that are dependent on it?