-1

I'm trying to create a dashboard displaying two plots:

A bar chart and a time series plot.

The time series plot has a slider and I would like the bar chart to only show values in the slider range.

I could use the shiny slider widget, but I would rather use one attached to the time series plot.

ismirsehregal
  • 30,045
  • 5
  • 31
  • 78
npennell
  • 15
  • 3

1 Answers1

0

Here is what I think you are after:

library(shiny)
library(plotly)
library(data.table)

nPoints <- 100
DT <- data.table(x = Sys.time()-seq_len(nPoints), y = runif(nPoints, 0, 10))

ui <- fluidPage(
    plotlyOutput("scatterPlot"),
    plotlyOutput("barPlot")
)

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

    output$scatterPlot <- renderPlotly({
        p_scatter <- plot_ly(
            DT,
            x = ~ x,
            y = ~ y,
            type = "scatter",
            mode = "lines"
        ) %>%
            layout(
                xaxis = list(
                    rangeslider = list(type = "date")
                    ))
    })

    xRangeRaw <- reactiveVal()
    xRange <- xRangeRaw %>% debounce(100)

    observeEvent(event_data("plotly_relayout"), {
        xRangeRaw(event_data("plotly_relayout")$xaxis.range)
        })

    output$barPlot <- renderPlotly({
        if(is.null(xRange())){
            filteredDT <- DT
        } else {
            filteredDT <- DT[x %between% as.POSIXct(xRange(), format = "%Y-%m-%d %H:%M:%OS")]
        }

        p_bar <- plot_ly(
            na.omit(filteredDT),
            x = ~ x,
            y = ~ y,
            type = "bar"
        )

    })
}

shinyApp(ui = ui, server = server)
ismirsehregal
  • 30,045
  • 5
  • 31
  • 78