0

I have a data frame which look like that :

Taux_retour = as.Date(c("2020-06-11", "2020-06-12", "2020-06-13", "2020-06-15", 
                                      "2020-06-16", "2020-06-17", "2020-06-18", "2020-06-19", 
                                      "2020-06-20", "2020-06-22", "2020-06-23", "2020-06-24", "2020-06-25"))

Taux_retour=as.data.frame(Taux_retour)
Taux_retour$n = c(183,94,3,233,247,200,181,125,3,144,155,146,116)
Taux_retour$`Retour (%)`= c(1.55,0.79, 0.03, 1.97, 2.09, 1.69, 1.53, 1.06, 0.03, 1.22, 1.31, 1.23, 0.98)

In my shiny web app, I'd like to make a plot and to print the data.frame used to create it juste under the graph. When my computer mouse is on a point, I'd like the related informations to appears. for exemple if I point the first one, want R to print the first line f the data.frame.

Here is the code :

ui = dashboardPage(
                fluidRow(
                box(title = "Evolution du taux de retour : 
                    (pour plus de détails, passez votre souris sur les points)",
                    status = "success",
                    width = 9, 
                    solidHeader = TRUE,
                    plotOutput('graphique', 
                               click = "image_click",
                               hover = hoverOpts(id = "plot_hover", delayType = "throttle")),
                    uiOutput("dynamic"))))
server = function(input, output) {
  
  output$graphique= renderPlot({
    ggplot(data=Taux_retour, aes(x=Taux_retour,y=`Retour (%)`))+geom_line(col="steelblue", lwd=1) +
      ylim(min(Taux_retour$`Retour (%)`), max(Taux_retour$`Retour (%)`)) + theme_light() + theme(legend.position='none') +
      labs(y = "Taux de retour (en %)", x="Dates") +
      geom_line() + geom_point()+
      geom_vline(xintercept=as.numeric(as.Date(env1)+1), color = "olivedrab") +
      annotate(geom="text",x=as.Date(env1_g),y=2.0,label="Premier envoi", fontface="bold", color = "#006600")
  })
  
  output$dynamic <- renderUI({
    hover <- input$plot_hover 
    y <- nearPoints(iris, input$plot_hover) # HERE
    req(nrow(y) != 0)
    verbatimTextOutput("vals")
  })
  
  output$vals <- renderPrint({
    hover <- input$plot_hover 
    y <- nearPoints(iris, input$plot_hover) # HERE
    req(nrow(y) != 0)
    y
  }) 
  
}
shinyApp(ui = ui, server = server)

The problem is that nothing happens when I'm mooving my mouse over the graph. I think I have to select some lines where it's written "# HERE" in the code but I can't get what ! If you have any ideas it would be great, I spend like 8 hours on that error ..

I'm also sorry for the errors in my language... You must have understood i'm not a nativ speacker..

so_so
  • 1

1 Answers1

0

You don't need this output$dynamic. Just put verbatimTextOutput("vals") in the UI.

The issue is due to the fact that you use iris in the nearPoints function, while the dataframe you plot is Taux_retour. Replace iris with Taux_retour.

EDIT

Here is the code. I commented some lines because you don't provide the objects env1 and env1_g.

ui = dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    fluidRow(
      box(title = "Evolution du taux de retour : 
        (pour plus de détails, passez votre souris sur les points)",
          status = "success",
          width = 9, 
          solidHeader = TRUE,
          plotOutput('graphique', 
                     click = "image_click",
                     hover = hoverOpts(id = "plot_hover", delayType = "throttle")),
          verbatimTextOutput("vals")
      )
    )
  )
)

server = function(input, output) {
  
  output$graphique= renderPlot({
    ggplot(data=Taux_retour, aes(x=Taux_retour,y=`Retour (%)`)) + 
      geom_line(col="steelblue", lwd=1) +
      ylim(min(Taux_retour$`Retour (%)`), max(Taux_retour$`Retour (%)`)) + 
      theme_light() + 
      theme(legend.position='none') +
      labs(y = "Taux de retour (en %)", x="Dates") +
      geom_line() + geom_point() #+
      # geom_vline(xintercept=as.numeric(as.Date(env1)+1), color = "olivedrab") +
      # annotate(geom="text",x=as.Date(env1_g),y=2.0,label="Premier envoi", fontface="bold", color = "#006600")
  })
  
  
  output$vals <- renderPrint({
    hover <- input$plot_hover 
    if(!is.null(hover)){
      points <- nearPoints(Taux_retour, hover) 
      if(nrow(points)) points
    }
  }) 
  
}

shinyApp(ui = ui, server = server)
Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225
  • Thanks for your help but it doesn't work anyway ... Did I made a mistake by replacing `uiOutput("dynamic")` (in the UI) by `verbatimTextOutput("vals")` ? – so_so Jul 09 '20 at 07:31
  • Well I tried your code but there is still no data printed while my mouse is pointing the curve. I can't get why does it works for you and not for me – so_so Jul 09 '20 at 09:24
  • @so_so For me the data is printed when I point the mouse to *a point of the curve*. – Stéphane Laurent Jul 09 '20 at 10:58
  • I found the issue : I didn't had the last R version. Since I downloaded it, it works ! Thank you very much for your help :) – so_so Jul 09 '20 at 11:05