title: Select variables from a dataframe after on which operations are made
body: <p>I'm constructing a dashboard with Shiny and for a certain plot I want the user let choose which variable the plot will show.
  So far I never had an issue with that but now, whyever, I always receive that the dataframe is not there to choose from.</p>
  <p>I guess this might be due to the circumstance that this dataframe is created rather at the end of the entire code and when Shiny is building up the dashboard including the SelectVariable option, it does not know this dataframe, yet.</p>
  <p>Does this make sense? On the other hand, I wonder then, why this had never been any issue before.
  If my guessing is true, how do I solve this?</p>
  <pre><code>ui.R
  library(shiny)
  
  ui&lt;- fluidPage(
    
    # Application title
    titlePanel(&quot;&quot;),
    
    # Application title
    titlePanel(&quot;HiRay 9 DB&quot;),
    
    tabsetPanel(               
      tabPanel(&quot;Histogram&quot;,
               sidebarLayout(
                    sidebarPanel(width = 3, sliderInput(&quot;bins&quot;
                                                     ,&quot;Number of bins:&quot;
                                                     ,min = 100
                                                     ,max = 1000
                                                     ,value = 500
                                                     ),
                                        varSelectInput(&quot;sel_variable&quot;
                                                   , &quot;Choose variable: &quot;
                                                   , data = df_sorted
                                                   , selected = &quot;x_ray_hours_balance&quot;
                                      #             , multiple = FALSE
                                                      )
                            ),
                     mainPanel(
                              plotOutput(&quot;histo&quot;, width = &quot;100%&quot;, height = &quot;500&quot;),
                              plotOutput(&quot;ggpairs&quot;, width = &quot;100%&quot;, height = &quot;500&quot;)
                              )
                          )
                ),
      tabPanel(&quot;Unsorted data&quot;, 
                mainPanel(
                          plotOutput(&quot;Unsorted_data&quot;, width = &quot;150%&quot;, height = &quot;650&quot;),
                          plotOutput(&quot;Unsorted_data2&quot;, width = &quot;150%&quot;, height = &quot;450&quot;)
                          )
               ),
      tabPanel(&quot;Sorted data&quot;, 
               mainPanel(
                          plotOutput(&quot;Sorted_data&quot;, width = &quot;150%&quot;, height = &quot;425&quot;),
                          plotOutput(&quot;Sorted_data2&quot;, width = &quot;150%&quot;, height = &quot;425&quot;)
                        )
              ),
      tabPanel(&quot;Weibull&quot;, 
               mainPanel(
                        div(&quot;Loading might take a while, please stay put..&quot;, style = &quot;color:blue&quot;)
                        , plotOutput(&quot;Weibull&quot;, width = &quot;150%&quot;, height = &quot;800&quot;)
                        )
              ),
      tabPanel(&quot;Survival&quot;, 
               mainPanel(
                          plotOutput(&quot;Survival&quot;, width = &quot;150%&quot;, height = &quot;800&quot;)
                        )
              ),
      tabPanel(&quot;Survival ext&quot;, 
               mainPanel(
                 plotOutput(&quot;Survival_ext&quot;, width = &quot;150%&quot;, height = &quot;800&quot;)
               )
              ),
      tabPanel(&quot;Correlations&quot;, 
               mainPanel(
                 plotOutput(&quot;Correlations&quot;, width = &quot;150%&quot;, height = &quot;800&quot;)
                        )
              ),
      tabPanel(&quot;PCA&quot;, 
               mainPanel(
                  plotOutput(&quot;PCA&quot;, width = &quot;150%&quot;, height = &quot;800&quot;)
                  )
              )
    )
  )
  
  
  
  server.R    
  # Read in data ----
  df = data.frame(read.csv(&quot;file.csv&quot;
                           , header = TRUE
                           , sep = &quot;;&quot;
                           , dec = &quot;,&quot;
                           , na.strings = &quot;---&quot;
                          )
                  )
  
  
  # clean data frame ----
  df&lt;- df %&gt;%
            clean_names()
  df&lt;- df %&gt;% janitor::remove_empty(whic=c(&quot;rows&quot;))
  df&lt;- df %&gt;% janitor::remove_empty(whic=c(&quot;cols&quot;))
  df&lt;- dplyr::distinct(df)
  colnames(df)[1]&lt;- &quot;country&quot;
  df_unsorted&lt;- df
  
  
  # sort out data ----
  ### keep only status, x_ray_hours_balance, preventive_exchange, operating_hours, op days, fail code
  df&lt;- df[, c(&quot;status&quot;
              , &quot;x_ray_hours_balance&quot;
              , &quot;preventive_exchange&quot;
              , &quot;position_in_unit&quot;
              , &quot;operating_hours_balance&quot;
              , &quot;operating_days&quot;
              , &quot;failure_code&quot;
  )]
  df&lt;- df[df$x_ray_hours_balance != &quot;---&quot;, ]   
  df&lt;- df[!is.na(df$x_ray_hours_balance), ]
  df&lt;- df[!is.na(df$status), ]
  
  
  # Make num columns numeric
  df[, c(
         &quot;x_ray_hours_balance&quot;
         , &quot;operating_hours_balance&quot;
         , &quot;operating_days&quot;
          )]&lt;- sapply(df[, c(
                             &quot;x_ray_hours_balance&quot;
                             , &quot;operating_hours_balance&quot;
                             , &quot;operating_days&quot;
                              )],as.numeric)
  
  ## keep x_ray &gt; 0 ----
  df&lt;- subset(df, df$x_ray_hours_balance &gt; 0)
  
  
  ## clean status ----
  df&lt;- df[df$status != &quot;?&quot;, ]   
  #df&lt;- df[df$position_in_unit != &quot;?&quot;, ]                                     
  df_sorted&lt;- df 
      
  
  # Plot ----
  server&lt;- function(input, output) {
  
      output$histo &lt;- renderPlot({
       p_1&lt;- ggplot(df_sorted, aes_string(x = input$sel_variable)) + 
       # p_1&lt;- ggplot(df_sorted, aes(x = input$sel_variable)) + 
       # p_1&lt;- ggplot(df_sorted, aes_string(x = x_ray_hours_balance )) + 
       # p_1&lt;- ggplot(df_sorted, aes(x = x_ray_hours_balance )) + 
           geom_histogram(binwidth = input$bins
                          , fill = &quot;darkgreen&quot;
                          , colour = &quot;forestgreen&quot;
           ) + theme_bw()
       p_1
       })
  }
  </code></pre>
  <p>When I replace <code>df_sorted</code> with <code>df</code> within the ggplot command, it works. The same/similar question arose already here: <a href="https://stackoverflow.com/questions/42535554/r-shiny-how-to-use-a-dataframe-variable-from-server-r-in-ui-r">R Shiny: how to use a dataframe variable from server.R in ui.R</a></p>