将子文件夹中的R文件实现为Shiny

时间:2015-09-30 06:28:54

标签: r shiny

我想在我的闪亮应用程序(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可能存在问题。

1 个答案:

答案 0 :(得分:0)

将'local = TRUE'添加到source()函数。