I have a shiny app which contains two files: app.R and functions.R
app.R:
# Setup ----
pacman::p_load(shiny, tidyverse, shinydashboard, lubridate, scales, DT)
source('functions.R', local = T)
# 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(
tabItems(
tabItem(tabName = "dh",
h2("DH Estimator"),
HTML("Adjust spend column for calculations"),
DT::DTOutput('example_ui_dh')
)
)
)
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 <- 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]] <<- v %>% as.numeric()
replaceData(dh_proxy, create_sample_df(budgets), resetPaging = FALSE)
})
}
shinyApp(ui, server)
And functions.R
# 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))))
}
If I open the project directory and run, I get an error message:
Error: could not find function "create_sample_df"
If before running the app I manually run the functions within functions.R in the console, then the app does indeed load.
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.
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.
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.
How can I structure my app so that it runs on first load?
Note that when the app loads you need to click the 'dh' tab to see the result.
[edit]
Upated code based on Akrun's comment, using myenv:
# Setup ----
pacman::p_load(shiny, tidyverse, shinydashboard, lubridate, scales, DT)
myenv <- new.env()
source('functions.R', local = myenv)
# 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(
tabItems(
tabItem(tabName = "dh",
h2("DH Estimator"),
HTML("Adjust spend column for calculations"),
DT::DTOutput('example_ui_dh')
)
)
)
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
str(info)
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)
sessionInfo:
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
