ggplot hoverOpts не работает должным образом по сравнению со старым стилем - PullRequest
0 голосов
/ 28 апреля 2019

Я пытаюсь построить функциональность наведения для моих графиков на основе кода, найденного здесь: ТАК вопрос в решении 3

функция зависания была изменена в ggplot2, но когда я изменяю

plotOutput("distPlot", hover = "plot_hover", hoverDelay = 0),

до

plotOutput("distPlot", hoverOpts(id = "plot_hover", delay = 0),

зависание не работает в половине случаев (кажется, пока я не щелкну где-нибудь. Я что-то здесь упускаю?

Также пытался добавить аргумент delayType, но, похоже, это не помогло.

library(shiny)
library(ggplot2)

ui <- fluidPage(

    tags$head(tags$style('
     #my_tooltip {
      position: absolute;
      width: 300px;
      z-index: 100;
      padding: 0;
     }
  ')),

    tags$script('
    $(document).ready(function() {
      // id of the plot
      $("#distPlot").mousemove(function(e) { 

        // ID of uiOutput
        $("#my_tooltip").show();         
        $("#my_tooltip").css({             
          top: (e.pageY + 5) + "px",             
          left: (e.pageX + 5) + "px"         
        });     
      });     
    });
  '),

    selectInput("var_y", "Y-Axis", choices = names(iris)),
    plotOutput("distPlot", hover = "plot_hover", hoverDelay = 0), ## issue is here
    uiOutput("my_tooltip")


)

server <- function(input, output) {


    output$distPlot <- renderPlot({
        req(input$var_y)
        ggplot(iris, aes_string("Sepal.Width", input$var_y)) + 
            geom_point()
    })

    output$my_tooltip <- renderUI({
        hover <- input$plot_hover 
        y <- nearPoints(iris, input$plot_hover)[input$var_y]
        req(nrow(y) != 0)
        wellPanel(dataTableOutput("vals"), style = 'background-color:#fff; padding:10px; width:400px;border-color:#339fff')
    })

    output$vals <- renderDataTable({
        hover <- input$plot_hover 
        y <- t(nearPoints(iris, input$plot_hover))
        req(nrow(y) != 0)
        DT::datatable(y, colnames = rep("", ncol(y)), options = list(dom = '', searching = F, bSort = FALSE))
    })  
}
shinyApp(ui = ui, server = server)

1 Ответ

0 голосов
/ 29 апреля 2019

рабочая версия с изменениями из комментариев:

library(shiny)
library(ggplot2)

ui <- fluidPage(

  tags$head(tags$style('
     #my_tooltip {
      position: absolute;
      width: 300px;
      z-index: 100;
      padding: 0;
     }
  ')),

  tags$script('
    $(document).ready(function() {
      // id of the plot
      $("#distPlot").mousemove(function(e) { 

        // ID of uiOutput
        $("#my_tooltip").show();         
        $("#my_tooltip").css({             
          top: (e.pageY + 5) + "px",             
          left: (e.pageX + 5) + "px"         
        });     
      });     
    });
  '),

  selectInput("var_y", "Y-Axis", choices = names(iris)),
  plotOutput("distPlot", hover = hoverOpts(id = "plot_hover", delay = 0)),
  uiOutput("my_tooltip")


)

server <- function(input, output) {


  output$distPlot <- renderPlot({
    req(input$var_y)
    ggplot(iris, aes_string("Sepal.Width", input$var_y)) + 
      geom_point()
  })

  output$my_tooltip <- renderUI({
    hover <- input$plot_hover 
    y <- nearPoints(iris, input$plot_hover)
    req(nrow(y) != 0)
    wellPanel(DT::dataTableOutput("vals"), style = 'background-color:#fff; padding:10px; width:400px;border-color:#339fff')
  })

  output$vals <- DT::renderDataTable({
    hover <- input$plot_hover 
    y <- nearPoints(iris, input$plot_hover)
    req(nrow(y)) != 0
    DT::datatable(t(y), colnames = rep("", ncol(t(y))), options = list(dom = 't', searching = F, bSort = FALSE))
  })  
}
shinyApp(ui = ui, server = server)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...