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

你。 提交于 2020-01-02 08:49:23

问题


I have made a shinydashboard app which has now quite an amount of lines of code, and I am wondering if there are ways to split the code into different . R files. I have seen a similar question here, but the answer does not help (especially it says nothing about the code in the server part of the app).

For the ui part, I have created functions called header, sidebar, and body, and then I merely write

ui <- dashboardPage(header(), sidebar(), body())

It works well, and it still works if the functions header, sidebar, and body need to have arguments.

For the server part, I don't think a similar strategy can be applied. I am wondering whether "local" server functions (one per menu item for instance) could be written, and then reunified into one central server function.

Do you think something like that is doable? More generally, thank you for your advice and ideas that could make my code more manageable.


回答1:


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.




回答2:


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.




回答3:


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


来源:https://stackoverflow.com/questions/31347470/how-to-manage-my-r-code-in-a-shiny-or-shinydashboard-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!