title: Shiny: passing reactive value to function and re-evalute function if reactive value changes
body: <p>I built a shiny dashboard, which takes an input file (as reactive) and creates some plots based on that file. As I did not want to rewrite all the code for barplots, histograms etc again and again, I created different functions for plotting bars, histograms etc.</p>
  <p>As an input these functions take processed data. Usually that means that I take my raw data (stored in an reactive variable), manipulate some values and create some kind of cross tabulated dataframe, which is passed to the plotting function.</p>
  <p>Everything works fine, except that the plots are not updated, if I change my input data. The reason for that seems to be that I first process my reactive data and then pass it to my function. Apparently one has to use the reactive variable in direct context with/inside the plot function to make the plot reactive too.</p>
  <p>Before I start re-writing my dashboard (an option that I really don't like), I wanted to ask if somebody knew an easy workaround to pass processed reactive variables to functions and still re-evaluate these functions, if the reactive value changes?</p>
  <p>As my code works, there is no need for a minimal example, but to make it easier to understand my problem, here is some kind of pseudo code</p>
  <pre><code># read selected xlsx file
  dat &lt;- shiny::reactive({
      readxl::read_xlsx(path=input$selected_file$datapath)
  })
  
  # function to plot data
  plot_bar &lt;- function(dat,
                       .x,
                       .y){
  
      # plot data
      plot(data=dat,x=.x,y=.y)
  }
  
  # call plot_bar
  plot_bar(dat=dat() %&gt;% 
               dplyr::count(age),
           .x=age,
           .y=n)
  
  </code></pre>
  <p>As Ronak Shah mentioned I might have been a bit too lazy not sharing a reproducible example. Sorry for that. I was hoping that plain text would do the trick as it's hard to keep it minimal with dashboards :D</p>
  <p>Anyways, here is some reproducible code. I hope this helps to clearify the problem.</p>
  <pre><code>
  library(shiny)
  
  # Define UI for application that draws a histogram
  ui &lt;- fluidPage(
  
      # Application title
      titlePanel(&quot;blupp&quot;),
  
      # Sidebar with a slider input for number of bins 
      sidebarLayout(
          sidebarPanel(
              selectInput(inputId=&quot;sel_tibble&quot;,
                          label=&quot;select tibble&quot;,
                          choices=c(&quot;test1&quot;,&quot;test2&quot;))
          ),
  
          # Show a plot of the generated distribution
          mainPanel(
              column(width=4,
                     plotOutput(outputId=&quot;barplot1&quot;)),
              column(width=4,
                     plotOutput(outputId=&quot;barplot2&quot;)),
              column(width=4,
                     plotOutput(outputId=&quot;barplot3&quot;))
          )
      )
  )
  
  # Define server logic required to draw a histogram 
  server &lt;- function(input, output) {
      
      # some data
      dat_list &lt;- list(test1=dplyr::tibble(X=1:10,
                                           Y=10:1,
                                           GRP1=sample(LETTERS[1:2],
                                                      size=10,
                                                      replace=T),
                                           GRP2=sample(LETTERS[5:6],
                                                       size=10,
                                                       replace=T)),
                       test2=dplyr::tibble(X=101:1000,
                                           Y=1000:101,
                                           GRP1=sample(LETTERS[1:2],
                                                      size=900,
                                                      replace=T),
                                           GRP2=sample(LETTERS[5:6],
                                                      size=900,
                                                      replace=T)))
      # Reactive: change between datasets (should affect plots)
      dat &lt;- reactive({
          input$sel_tibble
          res &lt;- dat_list[[input$sel_tibble]]
          return(res)
      })
      
      # Functions
      
      # passing processed reactive (plot won't change)
      plot_bar1 &lt;- function(dat,
                           .x,
                           .y,
                           id){
          # NSE
          .x &lt;- rlang::enquo(.x)
          .y &lt;- rlang::enquo(.y)
          
          # Plot Date
          output[[id]] &lt;- renderPlot({
              dat %&gt;%
                  ggplot2::ggplot(ggplot2::aes(x=!!.x,y=!!.y)) +
                  ggplot2::geom_col()
          })
      }
      
      # passing reactive and processing inside function (plot changes)
      plot_bar2 &lt;- function(dat,
                            .x,
                            id){
          # NSE
          .x &lt;- rlang::enquo(.x)
          
          # Plot Date
          output[[id]] &lt;- renderPlot({
              dat() %&gt;%
                  dplyr::count(!!.x) %&gt;%
                  ggplot2::ggplot(ggplot2::aes(x=!!.x,y=n)) +
                  ggplot2::geom_col()
          })
      }
      
      # Output
      plot_bar1(dat=dat() %&gt;%
                   dplyr::count(GRP1),
               .x=GRP1,
               .y=n,
               id=&quot;barplot1&quot;)
      
      plot_bar1(dat=dat() %&gt;%
                   dplyr::count(GRP2),
               .x=GRP2,
               .y=n,
               id=&quot;barplot2&quot;)
      plot_bar2(dat=dat,
                .x=GRP1,
                id=&quot;barplot3&quot;)
      
      
      
  }
  
  # Run the application 
  shinyApp(ui = ui, server = server)
  
  </code></pre>