title: How to make reactive covariate labels in shiny with gtsummary tbl_summary
body: <p>I'm attempting to made reactive labels for my summary table. I'm making a reactive regression model and then I'm pulling that data frame out of the model and using that to make my summary table using gtsummary::tbl_summary() which outputs prettier tables than stargazer imo.</p>
  <p>tbl_summary takes a list formatted like variable ~ &quot;variable name&quot;. I'm using if statements to check that the variable name exists in the df and if it does, it appends it to the temporary variable covars which ultimately makes up the reactive variable covar.labels. covar.labels should ultimately have a list of formulas formatted as variable ~ &quot;variable name&quot; which will define the labels for my summary table if and only if they actually exist in the regression model.</p>
  <p>Data: <a href="https://pastebin.com/kjAynKNH" rel="nofollow noreferrer">https://pastebin.com/kjAynKNH</a></p>
  <p>I tested this first outside of Shiny and the following code works:</p>
  <pre><code>library(shiny)
  library(gtsummary)
  library(gt)
  library(readr)
  library(dplyr)
  
  model_data &lt;- read_csv(&quot;model_df.csv&quot;)
  
  model &lt;- lm(perc_suspensions ~ Thefts, data=model_data)
  
  covars &lt;- vector()
      if ('perc_suspensions' %in% colnames(model$model)){
        covars &lt;- c(covars, perc_suspensions ~ 'perc_suspensions Test')
      } 
      if ('Thefts' %in% colnames(model$model)){
        covars &lt;- c(covars, Thefts ~ 'Thefts Test')
      } 
  
  
  tbl_summary(model$model,
                    label = covars) %&gt;% 
          as_gt()
  </code></pre>
  <p>It outputs this:</p>
  <p><a href="https://i.stack.imgur.com/E366o.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/E366o.png" alt="enter image description here" /></a></p>
  <p>So attempted it with Shiny:</p>
  <pre><code>library(shiny)
  library(gtsummary)
  library(gt)
  library(readr)
  library(dplyr)
  
  #Server
  # Loading in my dataframe
  data &lt;- read_csv(&quot;model_df.csv&quot;)
  
  
  server &lt;- function(input, output) {
    
    regFormula &lt;- reactive({
      as.formula(paste(&quot;perc_suspensions&quot;, &quot; ~ &quot;, paste(input$iv1, collapse = &quot;+&quot;)))
    }) #reactive for linear regression 
    
    # then, put that formula into lm() for a linear regression
    model &lt;- reactive({
      lm(regFormula(), data)
    })
  
    covar.label &lt;- reactive({
      covars &lt;- vector()
      if ('perc_suspensions' %in% colnames(model()$model)){
        covars &lt;- c(covars, perc_suspensions ~ 'perc_suspensions Test')
      } 
      if ('Thefts' %in% colnames(model()$model)){
        covars &lt;- c(covars, Thefts ~ 'Thefts Test')
      } 
      covars
    })
  
    #Create nice regression table output
    #stargazer() comes from the stargazer package
    output$regTab &lt;- renderText(
        {
      stargazer(model(), type=&quot;html&quot;)
    } # stargazer render
    )
    
    # Summary table
    output$summary &lt;- render_gt(
      {
        tbl_summary(model()$model,
                    label = covar.label) %&gt;% 
          as_gt()
      }
    )
  }
  
  </code></pre>
  <pre><code>ui &lt;- shinyUI(fluidPage(
    navbarPage(&quot;Final Exam App&quot;,
        tabPanel(
          &quot;Tab Panel PlaceHolder &quot;,
       headerPanel(&quot;Header Panel PlaceHolder&quot;),
        sidebarLayout(
          position = &quot;right&quot;,
          sidebarPanel(
            width=3,
            h2(&quot;Build your model&quot;),
            br(),
            checkboxGroupInput(
              &quot;iv1&quot;,
              label = &quot;Select any of the independent variables below to calculate your model. You can change your selection at any time.&quot;,
              c(&quot;Thefts per 100 students&quot; = &quot;Thefts&quot;)
            ) # checkboxGroupInput
          ), #sidebarpanel
          
          mainPanel(
            width = 9,
            br(),
                      fluidRow(
                        h3(&quot;Regression Table &amp; Summary Statistics&quot;),
                        HTML('&lt;/br&gt;'),
                        splitLayout(cellWidths = c(&quot;55%&quot;, &quot;45%&quot;),
                          cellArgs = list(style = &quot;padding: 6px&quot;),
                        tableOutput(&quot;regTab&quot;),
                        gt_output(&quot;summary&quot;)),
                        HTML('&lt;/br&gt;')), #fluidrow
                        HTML('&lt;/br&gt;')
                    )# mainPanel
        ) #sidebarlayout
      ) # regression + df tab panel
    ) #navbarpg
  ) # fluidpage
  ) #shinyUI
  </code></pre>
  <p>It only outputs the error: &quot;object of type 'closure' is not subsettable&quot;</p>
  <p>Any help is greatly appreciated!</p>