Home > Mobile >  Shiny app with nearPoints flashes data when scrollbar appears
Shiny app with nearPoints flashes data when scrollbar appears

Time:05-21

I'm trying to make an app which shows some data after the user clicks a point. It works, except that when the data is longer than the window the scrollbar shows up, resizing the plot and erasing the data. How to make the data show and stay?

Below the code of a minimal example.

library(shiny)
library(tidyr)

ui <- fluidPage(
  plotOutput("plot", click = "plot_click"),
  tableOutput("data")
)
server <- function(input, output, session) {
  output$plot <- renderPlot({
    ggplot(mtcars, aes(wt, mpg))   geom_point()
  }, res = 96)
  
  output$data <- renderTable({
    req(input$plot_click)
    np <- nearPoints(mtcars, input$plot_click) %>% 
      pull(gear)
    mtcars %>% 
      filter(gear == np)
  })
}

shinyApp(ui = ui, server = server)

CodePudding user response:

The problem here is, that once the vertical scrollbar shows up the plotOutput is resized and therefore re-rendered, this results in input$plot_click being reset to NULL causing an empty table.

We can use req()'s cancelOutput parameter to avoid this behaviour.

Please see ?req:

cancelOutput: If TRUE and an output is being evaluated, stop processing as usual but instead of clearing the output, leave it in whatever state it happens to be in.

library(shiny)
library(tidyr)
library(dplyr)
library(ggplot2)

ui <- fluidPage(
  plotOutput("plot", click = "plot_click"),
  tableOutput("data")
)
server <- function(input, output, session) {
  output$plot <- renderPlot({
    ggplot(mtcars, aes(wt, mpg))   geom_point()
  }, res = 96)
  
  output$data <- renderTable({
    req(input$plot_click, cancelOutput = TRUE)
    np <- nearPoints(mtcars, input$plot_click) %>% pull(gear)
    if(length(np) > 0){
      mtcars %>% filter(gear == np)
    } else {
      NULL
    }
  })
}

shinyApp(ui = ui, server = server)
  • Related