R 在屏幕中以绘图方式调整大小后获取轴范围

R 在屏幕中以绘图方式调整大小后获取轴范围,r,shiny,plotly,r-plotly,R,Shiny,Plotly,R Plotly,我有一个闪亮的应用程序,上面有时间序列数据的绘图。我希望用户能够平移x(时间)轴,但保持一天的一致窗口。为此,我需要在每次调整大小后获得当前的x轴范围。 为什么我不只是使用范围滑块?因为我有大约25000个数据点,使用范围滑块需要在应用程序初始化时将所有数据加载到绘图中,这会大大降低速度 您可以与plotly\u relayat一起使用。官方的详细的文档有一个 下面是一个显示plotly timeseries的xlimits的小示例。请注意,plotly\u relayout在最初呈现打印时将返

我有一个闪亮的应用程序,上面有时间序列数据的绘图。我希望用户能够平移x(时间)轴,但保持一天的一致窗口。为此,我需要在每次调整大小后获得当前的x轴范围。

为什么我不只是使用
范围滑块
?因为我有大约25000个数据点,使用
范围滑块
需要在应用程序初始化时将所有数据加载到绘图中,这会大大降低速度

您可以与
plotly\u relayat一起使用。官方的
详细的
文档有一个

下面是一个显示plotly timeseries的xlimits的小示例。请注意,
plotly\u relayout
在最初呈现打印时将返回
NULL
,在用户调整页面大小时返回打印尺寸,在用户双击自动缩放打印时返回TRUE

library(shiny)
library(plotly)

ui <- fluidPage(
    fluidRow(column(width = 6,
                    br(),
                    plotlyOutput("plot")),
             column(width = 6,
                    br(), br(),
                    htmlOutput("xlims"),
                    br(),
                    h4("Verbatim plotly `relayout` data"),
                    verbatimTextOutput("relayout")))
)

server <- function(input, output, session) {
    # create the plotly time series
  output$plot <- renderPlotly({
      today <- Sys.Date()
      tm <- seq(0, 600, by = 10)
      x <- today - tm
      y <- rnorm(length(x))
      p <- plot_ly(x = ~x, y = ~y, mode = 'lines', 
                   text = paste(tm, "days from today"), source = "source")
  })

  # print the xlims
  output$xlims <- renderText({
      zoom <- event_data("plotly_relayout", "source")
      # if plot just rendered, event_data is NULL
      # if user double clicks for autozoom, then zoom$xaxis.autorange is TRUE
      # if user resizes page, then zoom$width is pixels of plot width
      if(is.null(zoom) || names(zoom[1]) %in% c("xaxis.autorange", "width")) {
          xlim <- "default of plot"
      } else {
          xmin <- zoom$`xaxis.range[0]`
          xmax <- zoom$`xaxis.range[1]`
          xlim <- paste0("Min: ", xmin, "<br>Max: ", xmax)
      }
      paste0("<h4>X-Axis Limits:</h4> ", xlim)
  })

  # print the verbatim event_data for plotly_relayout
  output$relayout <- renderPrint({event_data("plotly_relayout", "source")})
}

shinyApp(ui, server)
库(闪亮)
图书馆(绘本)
ui您可以与
plotly\u relayout一起使用。官方的
详细的
文档有一个

下面是一个显示plotly timeseries的xlimits的小示例。请注意,
plotly\u relayout
在最初呈现打印时将返回
NULL
,在用户调整页面大小时返回打印尺寸,在用户双击自动缩放打印时返回TRUE

library(shiny)
library(plotly)

ui <- fluidPage(
    fluidRow(column(width = 6,
                    br(),
                    plotlyOutput("plot")),
             column(width = 6,
                    br(), br(),
                    htmlOutput("xlims"),
                    br(),
                    h4("Verbatim plotly `relayout` data"),
                    verbatimTextOutput("relayout")))
)

server <- function(input, output, session) {
    # create the plotly time series
  output$plot <- renderPlotly({
      today <- Sys.Date()
      tm <- seq(0, 600, by = 10)
      x <- today - tm
      y <- rnorm(length(x))
      p <- plot_ly(x = ~x, y = ~y, mode = 'lines', 
                   text = paste(tm, "days from today"), source = "source")
  })

  # print the xlims
  output$xlims <- renderText({
      zoom <- event_data("plotly_relayout", "source")
      # if plot just rendered, event_data is NULL
      # if user double clicks for autozoom, then zoom$xaxis.autorange is TRUE
      # if user resizes page, then zoom$width is pixels of plot width
      if(is.null(zoom) || names(zoom[1]) %in% c("xaxis.autorange", "width")) {
          xlim <- "default of plot"
      } else {
          xmin <- zoom$`xaxis.range[0]`
          xmax <- zoom$`xaxis.range[1]`
          xlim <- paste0("Min: ", xmin, "<br>Max: ", xmax)
      }
      paste0("<h4>X-Axis Limits:</h4> ", xlim)
  })

  # print the verbatim event_data for plotly_relayout
  output$relayout <- renderPrint({event_data("plotly_relayout", "source")})
}

shinyApp(ui, server)
库(闪亮)
图书馆(绘本)
用户界面