How to manage my R code in a shiny or shinydashboard app?

て烟熏妆下的殇ゞ 提交于 2019-12-06 04:11:05

I am not sure if this meets your requirement, you can create different files and do the required computations in those files and save all the objects (data frames or lists or literally anything) into .Rds file using saveRDS() in R and then load that file into server.R using loadRDS() which will have all your saved objects. You can find the documentation here.

Then simply use those objects by calling the names as they are saved earlier. Most of the complex Shiny apps use global.R file (just a general convention, you can use any name) to do the heavy computations and follow the above approach.

You can always use source to call other R files in server.R:

  1. Use source as you usually do in regular R outside any reactive functions.

  2. Use source("xxxxx", local=T) when you want to use it inside a reactive function so the r codes you called will run every time this piece of reactive codes are activated.

For the server side:

server.R:

library(shiny)
source('sub_server_functions.R')

function(input, output, session) {
    subServerFunction1(input, output, session)
    subServerFunction2(input, output, session)
    subServerFunction3(input, output, session) 
}

This has worked for me, it's possible you'll need to pass more variables to the subserver functions. But the scoping of the reactive output appears to allow this.

sub_server_functions.R:

subserverfunction1 <- function(input, output, session) {
  output$checkboxGroupInput1 <- renderUI({
    checkboxGroupInput('test1','test1',choices = c(1,2,3))
 })
}

subserverfunction2 <- function(input, output, session) {
  output$checkboxGroupInput2 <- renderUI({
    checkboxGroupInput('test2','test2',choices = c(1,2,3))
 })
}

subserverfunction3 <- function(input, output, session) {
  output$checkboxGroupInput3 <- renderUI({
    checkboxGroupInput('test3','test3',choices = c(1,2,3))
 })
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!