在Shiny应用程序中以交互方式(使用目标线)读取绘图值

时间:2015-07-10 11:40:29

标签: r plot shiny interactive

我基本上试图在Shiny应用中复制this网站上图表的行为。

也就是说,我想创建一个交互式图形,通过将鼠标光标悬停在图形上,您可以移动一个"目标线"沿x轴。然后,根据瞄准线的位置,图表上的绘图线的y值显示在瞄准线和绘图线的交点上。 (我打算发布一个说明性的数字,但看起来我还没有足够的声誉。)

我设法使应用程序正常运行。在我目前的实施中,我使用hover中的plotOutput选项获取光标在地块上的位置,然后使用abline向新地块添加定位线。与pointstext一起在图上添加y值。

我遇到的问题是,在移动一段时间后,定位线开始严重滞后于实际的鼠标光标。我认为这是因为每次鼠标悬停位置更新时都必须重绘整个绘图(当前光标移动时每隔500毫秒,因为我使用hoverOpts(delayType = "throttle"))。渲染速度不够快,无法跟上鼠标移动的步伐。我想知道是否有人知道如何解决这个问题。

Shiny app示例的可运行代码:

library(shiny)

trigWaves <- function(A = 1, ...) {
  xval <- seq(0, 2*pi, len = 201)
  sinx <- A * sin(xval); cosx <- A * cos(xval)

  plot(x = xval, y = sinx, type = 'n', ylab = "f(x)", xlab = "x", ...)
  abline(h = A * c(-1, 0, 1), lty = c(2, 1, 2), col = 'gray')
  abline(v = pi * seq(0, 2, by = 0.5), lty = 2, col = 'gray')
  lines(x = xval, y = sinx, col = 'red')
  lines(x = xval, y = cosx, col = 'blue')
  box()

  invisible(list(x = xval, y = list(sin = sinx, cos = cosx)))
}

# Maximum selectable amplitude
Amax <- 5


runApp(
  # Define UI for application
  list(ui = pageWithSidebar(

    # Application title
    headerPanel("Read Function Values Interactively from a Plot"),

    sidebarPanel(
      sliderInput("amplitude", 
                  "Amplitude:", 
                  min = 1,
                  max = Amax, 
                  value = 2,
                  step = 0.1)
    ),

    mainPanel(
      plotOutput("trigGraph",
                 hover =
                   hoverOpts(
                            id = "plothover",
                         delay = 500,
                     delayType = "throttle"
                   )
                 )

    )
  ),

  # Define server for application
  server = function(input, output, session) {

    A <- reactive(input$amplitude)
    hoverx <- reactiveValues(initial = 2)

    # Hover position
    tx <- reactive({

      # If no previous hover position found, return initial = 0
      if (is.null(hoverx$prev)) return(hoverx$initial)

      # Hover resets to NULL every time the plot is redrawn -
      # If hover is null, then use the previously saved hover value.
      if (is.null(input$plothover)) hoverx$prev else input$plothover$x

    })

    # Function to plot the 'reader line' and the function values
    readLine <- reactive({

       abline(v = tx(), col = 'gray'); box()

       # Plot coordinates for values and points
       pcoords <- list(x = rep(tx(), 2), y = A() * c(sin(tx()), cos(tx())))

       points(pcoords, pch = 16, col = c("red", "blue"))    # points on lines
       text(pcoords, labels = round(pcoords$y, 2), pos = 4) # function values

     })

    # Render the final output graph
    output$trigGraph <- renderPlot({

      # Create base plot
      trigWaves(A = A(), ylim = Amax * c(-1, 1))

      readLine() # Add the reader line and function values

      # Add a legend
      legend(x = 3.5, y = 0.9 * Amax, 
             legend = c("sin(x)", "cos(x)"),
             col = c("red", "blue"), lty = 1)

      # Save the hover position used as the previous position
      hoverx$prev <- tx()

    })

  }), display.mode= "showcase"
)

0 个答案:

没有答案