used on a smartphone, shiny interactive plot doesn't understand finger movements

前端 未结 2 1324
一整个雨季
一整个雨季 2020-12-29 22:54

I have a R-Shiny application with a plot that implements interactive actions: click, hovering (hovering is passing the mouse over the plot, which can be detected by shiny).

2条回答
  •  半阙折子戏
    2020-12-29 23:14

    While I can't solve the question completely, maybe a dirty workaround could also have some value to you. Or someone else can build on that answer.

    I could reproduce the error that the clicks on the plot are not captured on mobile. But I noticed that I can add additional clickevents with javascript/shinyjs.

    One way would be:

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

    It has a few drawbacks:

    • shapes are drawn with lines only, it does not capture all hovering on the plot
    • the position is quite imprecise since you have to account for borders and margins (very dirty but potential here)

    I ran a bit out of time after a few hours, one can for sure improve it, but maybe its of interest for you anyway.

    Test here: (link might change within next weeks)

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

    Reproducible code: (tested on smartphone: 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)
    

提交回复
热议问题