title: Could not find function within ShinyApp while using source
body: <p>I have a shiny app which contains two files: app.R and functions.R</p>
  <p>app.R:</p>
  <pre><code># Setup ----
  pacman::p_load(shiny, tidyverse, shinydashboard, lubridate, scales, DT)
  source('functions.R', local = T)
  
  
  # UI ----
  header &lt;- dashboardHeader(title = 'Velocity Spend &amp; Return Calculator')
  HTML(&quot;Adjust spend column for calculations&quot;)
  
  sidebar &lt;- dashboardSidebar(
    menuItem(&quot;dh&quot;, tabName = &quot;dh&quot;, icon = icon(&quot;dashboard&quot;))
  )
  
  body &lt;- dashboardBody(
    tabItems(
      tabItem(tabName = &quot;dh&quot;,
              h2(&quot;DH Estimator&quot;),
              HTML(&quot;Adjust spend column for calculations&quot;),
              DT::DTOutput('example_ui_dh')
              
      )
    )
  )
  
  
  ui &lt;- dashboardPage(header, sidebar, body)
  
  
  # Server ----
  server &lt;- function(input, output) {
    
    # Initial budgets, eventually set to come from dropdowns or user input
    budgets &lt;- list(
      '2020.4' = 1000000,
      '2021.1' = 1000000,
      '2021.2' = 1000000,
      '2021.3' = 1000000,
      '2021.4' = 1000000
    )
    
    dh_proxy = DT::dataTableProxy('example_ui_dh')
    
    # eventually use distinct budgets for each, just demo right now
    output$example_ui_dh &lt;- render_dt(budgets)
    
    # eventually make this a function
    # pass for now and just copy paste for demo
    # adding observeEvent to a function is not straightforwards
    
    # dh
    observeEvent(input$example_ui_dh_cell_edit, {
      info = input$example_ui_dh_cell_edit
      str(info)
      i = info$row
      j = info$col
      v = info$value
      budgets[[i]] &lt;&lt;- v %&gt;% as.numeric()
      replaceData(dh_proxy, create_sample_df(budgets), resetPaging = FALSE)
    })
    
  }
  shinyApp(ui, server)
  </code></pre>
  <p>And functions.R</p>
  <pre><code># generates an example df based on inputed budgets
  create_sample_df &lt;- function(budgets) {
      data.frame(cohort = seq('2020-10-01' %&gt;% ymd, '2021-12-31' %&gt;% ymd, by = '1 days')) %&gt;% 
      mutate(Quarter = quarter(cohort, with_year = T)) %&gt;% 
      add_count(Quarter) %&gt;% 
      mutate(DailyBudget = budgets[Quarter %&gt;% as.character] %&gt;% unlist / n) %&gt;% 
      group_by(Quarter) %&gt;% 
      mutate(Revenue = DailyBudget + rnorm(n(), mean = 0, sd = DailyBudget / 5)) %&gt;% 
      summarise(Spend = sum(DailyBudget),
                Revenue = sum(Revenue),
                .groups = 'drop') %&gt;% 
      mutate(Profit = dollar(Revenue - Spend),
             Payback = percent(Revenue / Spend),
             Spend = dollar(Spend),
             Revenue = dollar(Revenue)) %&gt;% 
      mutate(Quarter = as.character(Quarter)) # do this last keep ordering of quarters
  }
  
  
  
  # render DT
  render_dt &lt;- function(budgets, ...) {
    DT::renderDT(create_sample_df(budgets), editable = 'cell', server = T, 
                 list(target = 'column', disable = list(columns = c(0,2,3,4))))
  }
  </code></pre>
  <p>If I open the project directory and run, I get an error message:</p>
  <blockquote>
  <p>Error: could not find function &quot;create_sample_df&quot;</p>
  </blockquote>
  <p>If before running the app I manually run the functions within functions.R in the console, then the app does indeed load.</p>
  <p>Actually, I must run the code in functions.R in the console and then the variable budgets. Only if I do these two things will my app run. This is preventing me from publishing the app.</p>
  <p>I have tried sourcing the functions file within server as well as within observEvent, more out of acts of desperation. I did the same with creating the budgets variable.</p>
  <p>App runs fine when I have the functions and budgets variable loaded up in environment as variables, just not when running for the first time.</p>
  <p>How can I structure my app so that it runs on first load?</p>
  <p>Note that when the app loads you need to click the 'dh' tab to see the result.</p>
  <p>[edit]</p>
  <p>Upated code based on Akrun's comment, using myenv:</p>
  <pre><code># Setup ----
  pacman::p_load(shiny, tidyverse, shinydashboard, lubridate, scales, DT)
  myenv &lt;- new.env()
  source('functions.R', local  = myenv)
  
  
  # UI ----
  header &lt;- dashboardHeader(title = 'Velocity Spend &amp; Return Calculator')
  HTML(&quot;Adjust spend column for calculations&quot;)
  
  sidebar &lt;- dashboardSidebar(
    menuItem(&quot;dh&quot;, tabName = &quot;dh&quot;, icon = icon(&quot;dashboard&quot;))
  )
  
  body &lt;- dashboardBody(
    tabItems(
      tabItem(tabName = &quot;dh&quot;,
              h2(&quot;DH Estimator&quot;),
              HTML(&quot;Adjust spend column for calculations&quot;),
              DT::DTOutput('example_ui_dh')
              
      )
    )
  )
  
  
  ui &lt;- dashboardPage(header, sidebar, body)
  
  
  # Server ----
  server &lt;- function(input, output) {
    
    # Initial budgets, eventually set to come from dropdowns or user input
    budgets &lt;- list(
      '2020.4' = 1000000,
      '2021.1' = 1000000,
      '2021.2' = 1000000,
      '2021.3' = 1000000,
      '2021.4' = 1000000
    )
    
    dh_proxy = DT::dataTableProxy('example_ui_dh')
    
    # eventually use distinct budgets for each, just demo right now
    output$example_ui_dh &lt;- myenv$render_dt(budgets)
    
    # dh
    observeEvent(input$example_ui_dh_cell_edit, {
      info = input$example_ui_dh_cell_edit
      str(info)
      i = info$row
      j = info$col
      v = info$value
      budgets[[i]] &lt;&lt;- v %&gt;% as.numeric()
      replaceData(dh_proxy, myenv$create_sample_df(budgets), resetPaging = FALSE)
    })
    
  }
  shinyApp(ui, server)
  </code></pre>
  <p>sessionInfo:</p>
  <pre><code>sessionInfo()
  R version 3.6.0 (2019-04-26)
  Platform: x86_64-redhat-linux-gnu (64-bit)
  Running under: Amazon Linux 2
  
  Matrix products: default
  BLAS/LAPACK: /usr/lib64/R/lib/libRblas.so
  
  locale:
   [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
   [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                  LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
  
  attached base packages:
  [1] stats     graphics  grDevices utils     datasets  methods   base     
  
  other attached packages:
   [1] DT_0.14              scales_1.1.0         lubridate_1.7.4      shinydashboard_0.7.1 forcats_0.4.0        stringr_1.4.0        dplyr_1.0.2          purrr_0.3.4         
   [9] readr_1.3.1          tidyr_1.0.0          tibble_3.0.1         ggplot2_3.3.2        tidyverse_1.3.0      shiny_1.4.0         
  
  loaded via a namespace (and not attached):
   [1] Rcpp_1.0.4.6      lattice_0.20-38   assertthat_0.2.1  digest_0.6.25     packrat_0.5.0     mime_0.9          R6_2.4.1          cellranger_1.1.0  backports_1.1.8  
  [10] reprex_0.3.0      evaluate_0.14     httr_1.4.1        pillar_1.4.4      rlang_0.4.7       readxl_1.3.1      rstudioapi_0.11   rmarkdown_2.1     htmlwidgets_1.5.1
  [19] munsell_0.5.0     broom_0.5.3       compiler_3.6.0    httpuv_1.5.2      modelr_0.1.5      xfun_0.15         pkgconfig_2.0.3   htmltools_0.5.0   tidyselect_1.1.0 
  [28] fansi_0.4.1       withr_2.2.0       crayon_1.3.4      dbplyr_1.4.2      later_1.1.0.1     grid_3.6.0        nlme_3.1-143      jsonlite_1.7.0    xtable_1.8-4     
  [37] gtable_0.3.0      lifecycle_0.2.0   DBI_1.1.0         pacman_0.5.1      magrittr_1.5      cli_2.0.2         stringi_1.4.6     fs_1.4.2          promises_1.1.1   
  [46] xml2_1.3.2        ellipsis_0.3.1    generics_0.0.2    vctrs_0.3.4       tools_3.6.0       glue_1.3.2        crosstalk_1.1.0.1 hms_0.5.3         rsconnect_0.8.16 
  [55] fastmap_1.0.1     yaml_2.2.1        colorspace_1.4-1  rvest_0.3.5       knitr_1.29        haven_2.2.0      
  </code></pre>