Function not recognized when sourced in shiny (tried local=TRUE)

I posted elsewhere and when others run my app it seems to function as expected. I have an issue where for me, when I try to run I get a error message:

Error: [my function name] not found

All I can do is paste the app an the file that is imported along with sessionInfo() and hope that someone might know what's happening here:


pacman::p_load(shiny, dplyr, shinydashboard, lubridate, scales, DT)

myenv <- new.env()
source('functions.R', local  = myenv)
# initially tried source('functions.R', local  = TRUE) but same outcome

# UI ----
header <- dashboardHeader(title = 'Velocity Spend & Return Calculator')
HTML("Adjust spend column for calculations")

sidebar <- dashboardSidebar(
  menuItem("dh", tabName = "dh", icon = icon("dashboard"))

body <- dashboardBody(
    tabItem(tabName = "dh",
            h2("DH Estimator"),
            HTML("Adjust spend column for calculations"),

ui <- dashboardPage(header, sidebar, body)

# Server ----
server <- function(input, output) {
  # Initial budgets, eventually set to come from dropdowns or user input
  budgets <- 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 <- myenv$render_dt(budgets)
  # dh
  observeEvent(input$example_ui_dh_cell_edit, {
    info = input$example_ui_dh_cell_edit
    i = info$row
    j = info$col
    v = info$value
    budgets[[i]] <<- v %>% as.numeric()
    replaceData(dh_proxy, myenv$create_sample_df(budgets), resetPaging = FALSE)
shinyApp(ui, server)

Here is the file that is imported:

# generates an example df based on inputed budgets
create_sample_df <- function(budgets) {
    data.frame(cohort = seq('2020-10-01' %>% ymd, '2021-12-31' %>% ymd, by = '1 days')) %>% 
    mutate(Quarter = quarter(cohort, with_year = T)) %>% 
    add_count(Quarter) %>% 
    mutate(DailyBudget = budgets[Quarter %>% as.character] %>% unlist / n) %>% 
    group_by(Quarter) %>% 
    mutate(Revenue = DailyBudget + rnorm(n(), mean = 0, sd = DailyBudget / 5)) %>% 
    summarise(Spend = sum(DailyBudget),
              Revenue = sum(Revenue),
              .groups = 'drop') %>% 
    mutate(Profit = dollar(Revenue - Spend),
           Payback = percent(Revenue / Spend),
           Spend = dollar(Spend),
           Revenue = dollar(Revenue)) %>% 
    mutate(Quarter = as.character(Quarter)) # do this last keep ordering of quarters

# render DT
render_dt <- function(budgets, ...) {
  DT::renderDT(create_sample_df(budgets), editable = 'cell', server = T, 
               list(target = 'column', disable = list(columns = c(0,2,3,4))))

When I then run the app:

My project only has these two files:
Screen Shot 2020-09-11 at 3.31.22 PM

I tried ading print(myenv$create_sample_df) after the source() line just to ensure the function was being read in, and sure enough the function code did get printed out.

So, I am not sure why my function is not recognized. It's challenging because with the same code, others have been able to run this app fine, it's just me.

Session info:

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/

 [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 dplyr_1.0.2          shiny_1.4.0         

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.4.6      compiler_3.6.0    pillar_1.4.4      later_1.1.0.1     tools_3.6.0       digest_0.6.25     packrat_0.5.0     jsonlite_1.7.0    evaluate_0.14    
[10] lifecycle_0.2.0   tibble_3.0.1      pkgconfig_2.0.3   rlang_0.4.7       rstudioapi_0.11   crosstalk_1.1.0.1 yaml_2.2.1        xfun_0.15         fastmap_1.0.1    
[19] stringr_1.4.0     knitr_1.29        generics_0.0.2    vctrs_0.3.4       htmlwidgets_1.5.1 tidyselect_1.1.0  glue_1.3.2        R6_2.4.1          rmarkdown_2.1    
[28] pacman_0.5.1      purrr_0.3.4       magrittr_1.5      promises_1.1.1    htmltools_0.5.0   ellipsis_0.3.1    rsconnect_0.8.16  colorspace_1.4-1  mime_0.9         
[37] xtable_1.8-4      httpuv_1.5.2      stringi_1.4.6     munsell_0.5.0     crayon_1.3.4     

I am not sure what other info to add or what else to try? Any ideas and pointers most welcome.

Solved by editing functions.R. My call to one function, render_dt() was in turn calling another function, create_sample_df(). This seemed to be the issue. Solved by redefining render_dt() to not in turn call another custom function.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.