Disable Shiny Plots that Need to be Recalculated











up vote
2
down vote

favorite












So here's my problem:




  1. My shiny app has multiple graphs and maps and using the simple reactive model means it recalcs the plots ever time a checkbox is changed - too slow when the user may want to change more than one option.

  2. I've setup isolation and a "go button" as described in Stop reactions with isolate()

  3. I have more than one "row" of filters and graphs - basically I have two groups of filters that update their corresponding graphs.


The problem:




  • I want to show the user that the graphs are "out of date" when they change an input value so they are prompted to recalculate with the "go" button.


I've tried:




  • Using javascript handler on input/select change to add .recalculating to the plots. However when one of the go buttons is pressed, it recalculates only the plots related to that button BUT it removes the .recalculating class from ALL plots (even though some have not been recalculated).


Workaround is to have either go button update all plots but that's not ideal from a resource standpoint.



To reproduce use the code below and:




  1. Click both buttons to generate the graphs.

  2. Change both filters (checkbox and radio) which will show both graphs as needing recalculating.

  3. Press just one button to regenerate graphs.

  4. See that both graphs appear to be recalculated when only one has in fact changed.


Here's a working example of how my shiny app is organized:



library('shiny')
library('ggplot2')

ui <- fluidPage(
checkboxGroupInput(inputId = 'cyl', label = 'Cylinders:', choices = unique(mtcars$cyl), selected = unique(mtcars$cyl)),
actionButton('goButton', 'Update graph!'),
plotOutput('plot'),
radioButtons(inputId = 'vs', label = 'V-shaped 09:', choices = unique(mtcars$vs), selected = 1),
actionButton('goButton2', 'Update 2nd graph!'),
plotOutput('plot2'),
#change handlers
tags$script(HTML("$(document).on('change', 'input, select', function(event) {
$('.shiny-bound-output').addClass('recalculating')
})"))
)

server <- function(input, output) {
#first data.frame - cyl
df <- reactive({
mtcars[mtcars$cyl %in% input$cyl,]
})
#second data frame - vs9
df2 <- reactive({
mtcars[mtcars$vs == input$vs,]
})
output$plot <- renderPlot({
#only run if goButton pressed
if (input$goButton == 0)
return()
isolate({
ggplot(df(), aes(x=hp, y=disp)) +
geom_point()
})
})
output$plot2 <- renderPlot({
#only update if goButton2 pressed
if(input$goButton2 == 0)
return()
isolate({
ggplot(df2(), aes(x=hp, y=disp)) +
geom_point()
})
})
}

shinyApp(ui, server)









share|improve this question


























    up vote
    2
    down vote

    favorite












    So here's my problem:




    1. My shiny app has multiple graphs and maps and using the simple reactive model means it recalcs the plots ever time a checkbox is changed - too slow when the user may want to change more than one option.

    2. I've setup isolation and a "go button" as described in Stop reactions with isolate()

    3. I have more than one "row" of filters and graphs - basically I have two groups of filters that update their corresponding graphs.


    The problem:




    • I want to show the user that the graphs are "out of date" when they change an input value so they are prompted to recalculate with the "go" button.


    I've tried:




    • Using javascript handler on input/select change to add .recalculating to the plots. However when one of the go buttons is pressed, it recalculates only the plots related to that button BUT it removes the .recalculating class from ALL plots (even though some have not been recalculated).


    Workaround is to have either go button update all plots but that's not ideal from a resource standpoint.



    To reproduce use the code below and:




    1. Click both buttons to generate the graphs.

    2. Change both filters (checkbox and radio) which will show both graphs as needing recalculating.

    3. Press just one button to regenerate graphs.

    4. See that both graphs appear to be recalculated when only one has in fact changed.


    Here's a working example of how my shiny app is organized:



    library('shiny')
    library('ggplot2')

    ui <- fluidPage(
    checkboxGroupInput(inputId = 'cyl', label = 'Cylinders:', choices = unique(mtcars$cyl), selected = unique(mtcars$cyl)),
    actionButton('goButton', 'Update graph!'),
    plotOutput('plot'),
    radioButtons(inputId = 'vs', label = 'V-shaped 09:', choices = unique(mtcars$vs), selected = 1),
    actionButton('goButton2', 'Update 2nd graph!'),
    plotOutput('plot2'),
    #change handlers
    tags$script(HTML("$(document).on('change', 'input, select', function(event) {
    $('.shiny-bound-output').addClass('recalculating')
    })"))
    )

    server <- function(input, output) {
    #first data.frame - cyl
    df <- reactive({
    mtcars[mtcars$cyl %in% input$cyl,]
    })
    #second data frame - vs9
    df2 <- reactive({
    mtcars[mtcars$vs == input$vs,]
    })
    output$plot <- renderPlot({
    #only run if goButton pressed
    if (input$goButton == 0)
    return()
    isolate({
    ggplot(df(), aes(x=hp, y=disp)) +
    geom_point()
    })
    })
    output$plot2 <- renderPlot({
    #only update if goButton2 pressed
    if(input$goButton2 == 0)
    return()
    isolate({
    ggplot(df2(), aes(x=hp, y=disp)) +
    geom_point()
    })
    })
    }

    shinyApp(ui, server)









    share|improve this question
























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      So here's my problem:




      1. My shiny app has multiple graphs and maps and using the simple reactive model means it recalcs the plots ever time a checkbox is changed - too slow when the user may want to change more than one option.

      2. I've setup isolation and a "go button" as described in Stop reactions with isolate()

      3. I have more than one "row" of filters and graphs - basically I have two groups of filters that update their corresponding graphs.


      The problem:




      • I want to show the user that the graphs are "out of date" when they change an input value so they are prompted to recalculate with the "go" button.


      I've tried:




      • Using javascript handler on input/select change to add .recalculating to the plots. However when one of the go buttons is pressed, it recalculates only the plots related to that button BUT it removes the .recalculating class from ALL plots (even though some have not been recalculated).


      Workaround is to have either go button update all plots but that's not ideal from a resource standpoint.



      To reproduce use the code below and:




      1. Click both buttons to generate the graphs.

      2. Change both filters (checkbox and radio) which will show both graphs as needing recalculating.

      3. Press just one button to regenerate graphs.

      4. See that both graphs appear to be recalculated when only one has in fact changed.


      Here's a working example of how my shiny app is organized:



      library('shiny')
      library('ggplot2')

      ui <- fluidPage(
      checkboxGroupInput(inputId = 'cyl', label = 'Cylinders:', choices = unique(mtcars$cyl), selected = unique(mtcars$cyl)),
      actionButton('goButton', 'Update graph!'),
      plotOutput('plot'),
      radioButtons(inputId = 'vs', label = 'V-shaped 09:', choices = unique(mtcars$vs), selected = 1),
      actionButton('goButton2', 'Update 2nd graph!'),
      plotOutput('plot2'),
      #change handlers
      tags$script(HTML("$(document).on('change', 'input, select', function(event) {
      $('.shiny-bound-output').addClass('recalculating')
      })"))
      )

      server <- function(input, output) {
      #first data.frame - cyl
      df <- reactive({
      mtcars[mtcars$cyl %in% input$cyl,]
      })
      #second data frame - vs9
      df2 <- reactive({
      mtcars[mtcars$vs == input$vs,]
      })
      output$plot <- renderPlot({
      #only run if goButton pressed
      if (input$goButton == 0)
      return()
      isolate({
      ggplot(df(), aes(x=hp, y=disp)) +
      geom_point()
      })
      })
      output$plot2 <- renderPlot({
      #only update if goButton2 pressed
      if(input$goButton2 == 0)
      return()
      isolate({
      ggplot(df2(), aes(x=hp, y=disp)) +
      geom_point()
      })
      })
      }

      shinyApp(ui, server)









      share|improve this question













      So here's my problem:




      1. My shiny app has multiple graphs and maps and using the simple reactive model means it recalcs the plots ever time a checkbox is changed - too slow when the user may want to change more than one option.

      2. I've setup isolation and a "go button" as described in Stop reactions with isolate()

      3. I have more than one "row" of filters and graphs - basically I have two groups of filters that update their corresponding graphs.


      The problem:




      • I want to show the user that the graphs are "out of date" when they change an input value so they are prompted to recalculate with the "go" button.


      I've tried:




      • Using javascript handler on input/select change to add .recalculating to the plots. However when one of the go buttons is pressed, it recalculates only the plots related to that button BUT it removes the .recalculating class from ALL plots (even though some have not been recalculated).


      Workaround is to have either go button update all plots but that's not ideal from a resource standpoint.



      To reproduce use the code below and:




      1. Click both buttons to generate the graphs.

      2. Change both filters (checkbox and radio) which will show both graphs as needing recalculating.

      3. Press just one button to regenerate graphs.

      4. See that both graphs appear to be recalculated when only one has in fact changed.


      Here's a working example of how my shiny app is organized:



      library('shiny')
      library('ggplot2')

      ui <- fluidPage(
      checkboxGroupInput(inputId = 'cyl', label = 'Cylinders:', choices = unique(mtcars$cyl), selected = unique(mtcars$cyl)),
      actionButton('goButton', 'Update graph!'),
      plotOutput('plot'),
      radioButtons(inputId = 'vs', label = 'V-shaped 09:', choices = unique(mtcars$vs), selected = 1),
      actionButton('goButton2', 'Update 2nd graph!'),
      plotOutput('plot2'),
      #change handlers
      tags$script(HTML("$(document).on('change', 'input, select', function(event) {
      $('.shiny-bound-output').addClass('recalculating')
      })"))
      )

      server <- function(input, output) {
      #first data.frame - cyl
      df <- reactive({
      mtcars[mtcars$cyl %in% input$cyl,]
      })
      #second data frame - vs9
      df2 <- reactive({
      mtcars[mtcars$vs == input$vs,]
      })
      output$plot <- renderPlot({
      #only run if goButton pressed
      if (input$goButton == 0)
      return()
      isolate({
      ggplot(df(), aes(x=hp, y=disp)) +
      geom_point()
      })
      })
      output$plot2 <- renderPlot({
      #only update if goButton2 pressed
      if(input$goButton2 == 0)
      return()
      isolate({
      ggplot(df2(), aes(x=hp, y=disp)) +
      geom_point()
      })
      })
      }

      shinyApp(ui, server)






      r shiny






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 8 at 18:44









      Michael Tallino

      690314




      690314





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53214225%2fdisable-shiny-plots-that-need-to-be-recalculated%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53214225%2fdisable-shiny-plots-that-need-to-be-recalculated%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Xamarin.form Move up view when keyboard appear

          Post-Redirect-Get with Spring WebFlux and Thymeleaf

          Anylogic : not able to use stopDelay()