简体   繁体   English

R Shiny 仪表板 - 使用 source('file.R') 加载脚本

[英]R Shiny Dashboard - Loading Scripts using source('file.R')

Introduction介绍

I have created an R shiny dashboard app that is quickly getting quite complex.我创建了一个 R shiny 仪表板应用程序,它很快变得相当复杂。 I have over 1300 lines of code all sitting in app.R and it works.我在 app.R 中有超过 1300 行代码,它可以工作。 I'm using RStudio.我正在使用 RStudio。

My application has a sidebar and tabs and rather than using modules I dynamically grab the siderbar and tab IDs to generate a unique identifier when plotting graphs etc.我的应用程序有一个侧边栏和选项卡,而不是使用模块,我动态地获取侧边栏和选项卡 ID 以在绘制图形等时生成唯一标识符。

I'm trying to reorganise it to be more manageable and split it into tasks for other programmers but I'm running into errors.我正在尝试对其进行重组以使其更易于管理并将其拆分为其他程序员的任务,但我遇到了错误。

Working Code工作代码

My original code has a number of library statements and sets the working directory to the code location.我的原始代码有许多库语句,并将工作目录设置为代码位置。

rm(list = ls())
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
getwd()

I then have a range of functions that sit outside the ui/server functions so are only loaded once (not reactive).然后,我有一系列函数位于 ui/server 函数之外,因此只加载一次(不是响应式的)。 These are all called from within the server by setting the reactive values and calling the functions from within something like a renderPlot.这些都是从服务器内部调用的,方法是设置响应值并从像 renderPlot 之类的内部调用函数。 Some of them are nested, so a function in server calls a function just in regular app.R which in turn calls another one.其中一些是嵌套的,因此服务器中的 function 调用 function 只是在常规 app.R 中调用另一个。 Eg.例如。

# Start of month calculation
som <- function(x) {
  toReturn <- as.Date(format(x, "%Y-%m-01"))
  return(toReturn)
}

start_fc <- function(){
  fc_start_date <- som(today())
  return(fc_start_date)
}

then in server something like this (code incomplete)然后在服务器中像这样(代码不完整)

server <- function(input, output, session) {
  RV <- reactiveValues()
  observe({
    RV$selection <- input[[input$sidebar]]
  #  cat("Selected:",RV$selection,"\r")
  })
  
.......
    cat(paste0("modelType: ",input[[paste0(RV$selection,"-modeltype")]]," \n"))
    vline1 <- decimal_date(start_pred(input[[paste0(RV$selection,"-modeltype")]],input[[paste0(RV$selection,"-modelrange")]][1]))
    vline2 <- decimal_date(start_fc())
.......

Problem Code问题代码

So now when I take all my functions and put them into different.R files I get errors indicating the functions haven't been loaded.因此,现在当我将所有函数放入不同的.R 文件时,我收到错误,表明函数尚未加载。 If I load the source files by highlighting them and Alt-Enter running them so they are loaded into memory then click on Run App the code works.如果我通过突出显示源文件来加载源文件并按 Alt-Enter 运行它们,以便将它们加载到 memory 然后单击运行应用程序代码有效。 But if I rely on Run App to load those source files, and the functions within them, the functions can't be found.但是,如果我依靠 Run App 来加载这些源文件以及其中的函数,则找不到这些函数。

source('./functionsGeneral.R')
source('./functionsQuote.R')
source('./functionsNewBusiness.R')

source('./ui.R')
source('./server.R')

shinyApp(ui, server)

where ui.R is其中 ui.R 是

source('./header.R')
source('./sidebar.R')
source('./body.R')

source('./functionsUI.R')

ui <- dashboardPage( 
  header,
  sidebar,
  body
)

Finally the questions最后的问题

In what order does R Shiny Dashboard run the code. R Shiny Dashboard 以什么顺序运行代码。 Why does it fail when I put the exact same inline code into another file and reference it with source('./functions.R')?当我将完全相同的内联代码放入另一个文件并使用 source('./functions.R') 引用它时,为什么它会失败? Does it not load into memory during a shiny app session?在 shiny 应用程序 session 期间,它不会加载到 memory 中吗? What am I missing?我错过了什么?

Any help on this would be greatly appreciated.对此的任何帮助将不胜感激。

Thanks, Travis谢谢,特拉维斯

Ok I've discovered the easiest way is to create a subfolder called R and to place the preload code into that folder.好的,我发现最简单的方法是创建一个名为 R 的子文件夹,并将预加载代码放入该文件夹中。 From shiny version 1.5 all this code in the R folder is loaded first automatically.从 shiny 版本 1.5 开始,R 文件夹中的所有代码都会首先自动加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM