如何在具备图表切片过滤与计数器的Shiny应用中设置非0起始动态v_line
Got it, let's tackle adding that dynamic vertical reference line (that doesn't start at 0) to your Shiny app! Here's a complete, working implementation tailored to your existing code structure:
Step-by-Step Implementation
1. Full Updated Code
First, here's the full app code with the dynamic vertical line integrated:
library(shiny) ui <- fluidPage( div(style="display:inline-block", numericInput("start_time", "Starting time:", 1, min = 1, max = 100)), div(style="display:inline-block", numericInput("stop_time", "Stop time:", 5, min = 1, max = 100)), actionButton("start_counter","Start the counter"), plotOutput("plot_timeseries", width = "500px", height = "300px"), plotOutput("plot_timeseries2", width = "500px", height = "300px") ) server <- function(input, output, session) { # Reactive value to track the dynamic vertical line (starts at input$start_time, NOT 0) current_vline <- reactiveVal(input$start_time) # Flag to track if counter is running counter_active <- reactiveVal(FALSE) # Observer for counter start/updates observeEvent(input$start_counter, { counter_active(TRUE) # Reset reference line to the user's chosen start time current_vline(input$start_time) # Recursive function to increment counter and update v-line update_counter <- function() { req(counter_active()) current_val <- current_vline() if(current_val < input$stop_time) { current_vline(current_val + 1) # Update every 1 second (adjust the 1000 ms value to change speed) invalidateLater(1000, session) } else { counter_active(FALSE) } } # Kick off the counter loop update_counter() }) # Render first plot with dynamic vertical line output$plot_timeseries <- renderPlot({ # Replace this with your actual filtered time series data sample_data <- data.frame(time = 1:100, value = rnorm(100)) plot(sample_data$time, sample_data$value, type = "l", xlab = "Time", ylab = "Value", main = "Time Series with Dynamic V-Line") # Add the dynamic vertical reference line abline(v = current_vline(), col = "darkred", lwd = 2, lty = 2) }) # Render second plot (repeat v-line logic as needed) output$plot_timeseries2 <- renderPlot({ sample_data2 <- data.frame(time = 1:100, value = runif(100)) plot(sample_data2$time, sample_data2$value, type = "b", xlab = "Time", ylab = "Value", main = "Second Plot with Dynamic V-Line") abline(v = current_vline(), col = "steelblue", lwd = 2, lty = 2) }) } shinyApp(ui, server)
2. Key Details Explained
- Non-Zero Initialization: The
current_vlinereactive value starts atinput$start_timeinstead of 0, exactly matching your requirement. - Dynamic Updates: The
observeEventtriggers when the "Start the counter" button is clicked. It resets the reference line, then increments it every second (adjustinvalidateLater(1000)to make it faster/slower) until it hits thestop_time. - Reactive Plot Sync: Since
current_vlineis a reactive value, every time it changes, both plots automatically re-render with the updated vertical line position. - Customization: Tweak the line's color (
col), thickness (lwd), style (lty), or replace the sample data with your actual filtered chart slices from the app.
内容的提问来源于stack exchange,提问作者Henk Straten
相关产品推荐
相关产品推荐

