来自智能手机的闪亮互动图无法理解手指的移动

age*_*nis 21 r smartphone click hover shiny

我有一个R-Shiny应用程序,该应用程序的图实现了交互式操作:单击,悬停(悬停将鼠标移到该图上,可以通过闪亮检测到)。为了给出一个想法,我在下面发布了一个简化的Shinyapp,它具有对我来说有问题的功能,即交互式绘图。(它是从我的一个老答案采取这里

它实际上运行良好,但是我需要人们从智能手机上使用它。问题是:我们在智能手机中所做的手指移动被手机解释为在页面上缩放或在页面上滚动,而不是鼠标选择或情节上的鼠标移动(悬停)。

我可以在应用程序上实现将触摸事件转换为鼠标事件的代码(java?css?)的修改,还是智能手机上的选项/手势可以实现类似鼠标的移动?

非常感谢。码:

library(shiny)
ui <- fluidPage(
  h4("Click on plot to start drawing, click again to pause"),
  sliderInput("mywidth", "width of the pencil", min=1, max=30, step=1, value=10),
  actionButton("reset", "reset"),
  plotOutput("plot", width = "500px", height = "500px",
             hover=hoverOpts(id = "hover", delay = 100, delayType = "throttle", clip = TRUE, nullOutside = TRUE),
             click="click"))
server <- function(input, output, session) {
  vals = reactiveValues(x=NULL, y=NULL)
  draw = reactiveVal(FALSE)
  observeEvent(input$click, handlerExpr = {
    temp <- draw(); draw(!temp)
    if(!draw()) {
      vals$x <- c(vals$x, NA)
      vals$y <- c(vals$y, NA)
    }})
  observeEvent(input$reset, handlerExpr = {
    vals$x <- NULL; vals$y <- NULL
  })
  observeEvent(input$hover, {
    if (draw()) {
      vals$x <- c(vals$x, input$hover$x)
      vals$y <- c(vals$y, input$hover$y)
    }})
  output$plot= renderPlot({
    plot(x=vals$x, y=vals$y, xlim=c(0, 28), ylim=c(0, 28), ylab="y", xlab="x", type="l", lwd=input$mywidth)
  })}
shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)

gre*_*g L 7

您可以使用touch-actionCSS 属性禁用绘图上的平移/缩放手势:

#plot {
  touch-action: none;
}
Run Code Online (Sandbox Code Playgroud)

将触摸事件转换为鼠标事件有点棘手,但您可以监听诸如touchstart, 之类的触摸事件touchmovetouchend并在 JavaScript 中模拟等效的鼠标事件。有关更多信息,请参阅https://developer.mozilla.org/en-US/docs/Web/API/Touch_events/Using_Touch_Eventshttps://javascript.info/dispatch-events

这并不完美,但我试了一下。我禁用了绘图上的触摸手势并添加了一个脚本,该脚本转换touchmovemousemove,并告诉服务器何时开始绘图 (on touchstart) 和停止绘图 (on touchend)。

library(shiny)

ui <- fluidPage(
  h4("Click on plot to start drawing, click again to pause"),
  sliderInput("mywidth", "width of the pencil", min=1, max=30, step=1, value=10),
  actionButton("reset", "reset"),
  plotOutput("plot", width = "400px", height = "400px",
             hover=hoverOpts(id = "hover", delay = 100, delayType = "throttle", clip = TRUE, nullOutside = TRUE),
             click="click"),
  tags$head(
    tags$script("
      $(document).ready(function() {
        var plot = document.getElementById('plot')

        plot.addEventListener('touchmove', function (e) {
          var touch = e.changedTouches[0];
          var mouseEvent = new MouseEvent('mousemove', {
            view: window,
            bubbles: true,
            cancelable: true,
            screenX: touch.screenX,
            screenY: touch.screenY,
            clientX: touch.clientX,
            clientY: touch.clientY
          })
          touch.target.dispatchEvent(mouseEvent);
          e.preventDefault()
        }, { passive: false });

        plot.addEventListener('touchstart', function(e) {
          Shiny.onInputChange('draw', true)
          e.preventDefault()
        }, { passive: false });

        plot.addEventListener('touchend', function(e) {
          Shiny.onInputChange('draw', false)
          e.preventDefault()
        }, { passive: false });
      })
    "),
    tags$style("#plot { touch-action: none; }")
    )
)

server <- function(input, output, session) {
  vals = reactiveValues(x=NULL, y=NULL)
  draw = reactiveVal(FALSE)

  observeEvent(input$click, {
    draw(!draw())
    vals$x <- append(vals$x, NA)
    vals$y <- append(vals$y, NA)
  })

  observeEvent(input$draw, {
    draw(input$draw)
    vals$x <- append(vals$x, NA)
    vals$y <- append(vals$y, NA)
  })

  observeEvent(input$reset, handlerExpr = {
    vals$x <- NULL; vals$y <- NULL
  })

  observeEvent(input$hover, {
    if (draw()) {
      vals$x <- c(vals$x, input$hover$x)
      vals$y <- c(vals$y, input$hover$y)
    }
  })

  output$plot= renderPlot({
    plot(x=vals$x, y=vals$y, xlim=c(0, 28), ylim=c(0, 28), ylab="y", xlab="x", type="l", lwd=input$mywidth)
  })
}

shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)


Big*_*ist 5

尽管我无法完全解决问题,但肮脏的解决方法也可能对您有所帮助。或者其他人可以基于该答案。

我可以重现以下错误:未在移动设备上捕获图表上的点击。但是我注意到我可以使用javascript / shinyjs添加其他clickevent。

一种方法是:

  onevent(event = "click", id = "plot", function(e){
    global$clickx = c(global$clickx, e$pageX - 88)
    global$clicky = c(global$clicky, 540 - e$pageY)
  })
Run Code Online (Sandbox Code Playgroud)

它有一些缺点:

  • 形状仅用线绘制,它不能捕获绘图上的所有悬停
  • 该位置非常不准确,因为您必须考虑边界和边距(非常脏,但这里有潜力)

几个小时后,我的时间用完了,可以肯定可以改善它,但是无论如何您可能都会感兴趣。

在此处进行测试:(链接可能在接下来的几周内发生变化)

http://ec2-3-121-215-255.eu-central-1.compute.amazonaws.com/shiny/rstudio/sample-apps/mobile/

可复制的代码:(在智能手机上测试:Mi A2)

library(shiny)
library(shinyjs)
ui <- fluidPage(
  useShinyjs(),
  h4("Click on plot to start drawing, click again to pause"),

  plotOutput(outputId = "plot", width = "500px", height = "500px")
)


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

  onevent(event = "click", id = "plot", function(e){
    global$clickx = c(global$clickx, e$pageX - 88)
    global$clicky = c(global$clicky, 540 - e$pageY)
  })

  global <- reactiveValues(clickx = NULL, clicky = NULL)

  output$plot= renderPlot({
    plot(x = NULL, y = NULL, xlim=c(0, 440), ylim=c(0, 440), ylab="y", xlab="x", type="l")
    len <- length(global$clickx)
    lines(x = global$clickx, y = global$clicky, type = "p")      
    if(len > 1){
      for(nr in 2:len){
        lines(x = global$clickx[(nr - 1):nr], y = global$clicky[(nr - 1):nr], type = "l")
      }
    }
  })
}
shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)