我想在我的闪亮应用程序(http://code.tutsplus.com/tutorials/3-key-software-principles-you-must-understand--net-25161)中坚持使用KISS(保持简单,愚蠢)的原则。
因此,我创建了一个文件夹和子文件夹,并将server.R
中使用的代码保存在函数和.R
文件中。
这里有两个保存的.R
文件的两个示例:
output$myChoices <- renderUI({
selectInput(inputId = 'x',
label = 'y',
choices = levels(myDataSet$df$z),
multiple = T
)
})
或
absoluteMatrix <- reactive({
lifeChar <- switch(as.integer(input$something),
"abc",
"def",
"ghi",
"jkl",
"mno"
)
if( (myBoolean$absolute == TRUE) && (length(input$xy) != 0) ){
if( (length(input$gh) == 0) ) {
return(myData()[,grepl(lifeChar, names(myData()))] %>%
na.omit %>%
as.matrix)
} else if( (length(input$gh) != 0) ) {
return(myDataX()[,grepl(lifeChar, names(myDataX()))] %>%
na.omit %>%
as.matrix)
}
}
})
在server.R
我写了以下内容
library(shiny)
source("onLoad.R")
shinyServer(function(input, output, session) {
sourceRecursive("/.../")
})
在onLoad.R
文件中定义了函数sourceRecursive
:
#check folder and all subfolders for .R files
#source() them!
sourceRecursive <- function(path) {
dirs <- list.dirs()
files <- dir(pattern = "^.*[Rr]$", include.dirs = FALSE)
for (f in files)
source(f)
for (d in dirs)
sourceRecursive(d)
}
但是,这并没有成功。它似乎没有加载文件,或者内容的reactivity
可能存在问题。
答案 0 :(得分:0)
将'local = TRUE'添加到source()函数。