1

I want to stick with the KISS (keep it simple, stupid) principle within my shiny app (http://code.tutsplus.com/tutorials/3-key-software-principles-you-must-understand--net-25161).

So I created a folder and subfolders and saved peaces of my code uses in the server.R into functions and .R files. Here two examples of two saved .R files:

output$myChoices <- renderUI({
    selectInput(inputId = 'x', 
                label = 'y', 
                choices = levels(myDataSet$df$z),
                multiple = T
    )
})

or

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)
        } 
    }
})

In the server.R I wrote the following

library(shiny)
source("onLoad.R")

  shinyServer(function(input, output, session) {

     sourceRecursive("/.../")

 })

In the onLoad.R file the function sourceRecursive is defined:

 #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)
   }

But, that did not do the trick. It seems that it does not load the files or there might problems with the reactivity of their content.

four-eyes
  • 10,740
  • 29
  • 111
  • 220

1 Answers1

0

add 'local = TRUE' to the source() function.

Bogdan Rau
  • 625
  • 5
  • 17