R 使用源代码时在ShinyApp中找不到函数

R 使用源代码时在ShinyApp中找不到函数,r,shiny,R,Shiny,我有一个闪亮的应用程序,它包含两个文件:app.R和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(Quart

我有一个闪亮的应用程序,它包含两个文件:app.R和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))))
}
附录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)

新环境中的功能正在运行

-输出

-会议

sessionInfo()
R version 4.0.2 (2020-06-22)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Catalina 10.15.6

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib

locale:
[1] en_CA.UTF-8/en_CA.UTF-8/en_CA.UTF-8/C/en_CA.UTF-8/en_CA.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] DT_0.14              lubridate_1.7.9      shinydashboard_0.7.1 sjlabelled_1.1.6     scales_1.1.1         labelled_2.6.0       haven_2.3.1         
 [8] tidyr_1.1.0          ggplot2_3.3.2        dplyr_1.0.1          questionr_0.7.2      shiny_1.5.0         

loaded via a namespace (and not attached):
 [1] tidyselect_1.1.0  purrr_0.3.4       snakecase_0.11.0  colorspace_1.4-1  vctrs_0.3.2       generics_0.0.2    miniUI_0.1.1.1    htmltools_0.5.0  
 [9] yaml_2.2.1        rlang_0.4.7       later_1.1.0.1     pillar_1.4.6      glue_1.4.1        withr_2.2.0       lifecycle_0.2.0   munsell_0.5.0    
[17] gtable_0.3.0      htmlwidgets_1.5.1 labeling_0.3      forcats_0.5.0     fastmap_1.0.1     httpuv_1.5.4      crosstalk_1.1.0.1 curl_4.3         
[25] fansi_0.4.1       highr_0.8         Rcpp_1.0.5        xtable_1.8-4      readr_1.3.1       promises_1.1.1    jsonlite_1.7.0    mime_0.9         
[33] farver_2.0.3      hms_0.5.3         digest_0.6.25     stringi_1.4.6     insight_0.9.0     grid_4.0.2        cli_2.0.2         tools_4.0.2      
[41] magrittr_1.5      tibble_3.0.3      pacman_0.5.1      crayon_1.3.4      pkgconfig_2.0.3   ellipsis_0.3.1    assertthat_0.2.1  rstudioapi_0.11  
[49] R6_2.4.1          compiler_4.0.2   
-代码

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

myenv <- new.env()
source(file.path(getwd(), 'Downloads/functions_new.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)
  
  print(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)
pacman::p_负载(闪亮、dplyr、闪亮仪表板、润滑、天平、DT)

我没有?不知道那是什么?我使用T作为True的rstudio速记,只需将文件functions.R重命名为global.R即可。那么你就不需要源代码了。好吧,根据它将速记t改为TRUEBased应该可以工作是的,看起来确实是同一个问题,但即使在我对源代码的调用中添加local=TRUE,也会导致函数不可用时的相同错误found@DougFir从您的会话信息,我发现作为1.4.0版本的
shinny
,我已经更新了1.5.0版本的所有软件包,shinny现在是1.5版本。。。问题依然存在though@DougFir对不起,我希望我能有一个答案,我会做研究,如果我发现了什么,我会更新你的,很有趣。我以前从未这样做过,但我会在周一试一试。谢谢你的建议,我会在这里更新的…我通过编辑我的函数解决了这个问题。在我的原始函数中,我的函数
render\u dt
上面的.R依次调用另一个函数
create\u sample\u df
。这似乎就是问题所在。因此,我将定义更改为
render_dt=function(data,editable='cell',server=TRUE,…){renderDT(data,selection='none',server=server,editable=editable,…)}
然后像这样调用它
render_dt(create_sample_df(预算),rownames=FALSE,list(目标='列',…)
sessionInfo()
R version 4.0.2 (2020-06-22)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Catalina 10.15.6

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib

locale:
[1] en_CA.UTF-8/en_CA.UTF-8/en_CA.UTF-8/C/en_CA.UTF-8/en_CA.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] DT_0.14              lubridate_1.7.9      shinydashboard_0.7.1 sjlabelled_1.1.6     scales_1.1.1         labelled_2.6.0       haven_2.3.1         
 [8] tidyr_1.1.0          ggplot2_3.3.2        dplyr_1.0.1          questionr_0.7.2      shiny_1.5.0         

loaded via a namespace (and not attached):
 [1] tidyselect_1.1.0  purrr_0.3.4       snakecase_0.11.0  colorspace_1.4-1  vctrs_0.3.2       generics_0.0.2    miniUI_0.1.1.1    htmltools_0.5.0  
 [9] yaml_2.2.1        rlang_0.4.7       later_1.1.0.1     pillar_1.4.6      glue_1.4.1        withr_2.2.0       lifecycle_0.2.0   munsell_0.5.0    
[17] gtable_0.3.0      htmlwidgets_1.5.1 labeling_0.3      forcats_0.5.0     fastmap_1.0.1     httpuv_1.5.4      crosstalk_1.1.0.1 curl_4.3         
[25] fansi_0.4.1       highr_0.8         Rcpp_1.0.5        xtable_1.8-4      readr_1.3.1       promises_1.1.1    jsonlite_1.7.0    mime_0.9         
[33] farver_2.0.3      hms_0.5.3         digest_0.6.25     stringi_1.4.6     insight_0.9.0     grid_4.0.2        cli_2.0.2         tools_4.0.2      
[41] magrittr_1.5      tibble_3.0.3      pacman_0.5.1      crayon_1.3.4      pkgconfig_2.0.3   ellipsis_0.3.1    assertthat_0.2.1  rstudioapi_0.11  
[49] R6_2.4.1          compiler_4.0.2   
pacman::p_load(shiny, dplyr, shinydashboard, lubridate, scales, DT)

myenv <- new.env()
source(file.path(getwd(), 'Downloads/functions_new.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)
  
  print(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)