Split big shinydashboard app into pieces

一个人想着一个人 提交于 2019-12-07 20:18:45

问题


I'm quite new to shiny and shinydashboard. My first application has grown to a size that I would like to refactor it into pieces as hinted http://rstudio.github.io/shinydashboard/structure.html here:

dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody()
)

It should be a fairly simple task. However, I could not find any examples on how to split my app into multiple files, and I am not sure what is the best way to do this.

I could not get it to work so far: I tried calling source("myBody.R") within each part.


回答1:


  1. Take a look at @Shape's strategy in https://stackoverflow.com/a/33584292/4606130.

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

Other ideas are:

  1. Put your data calls and constants in a global.R which can be shared by both your ui and server.R file. Look at http://shiny.rstudio.com/articles/scoping.html

  2. Look at the new Module approach to Shiny. I am still getting to grips with this but looks promising to rationalise. See http://shiny.rstudio.com/articles/modules.html

An example of a flex dashboard .Rmd file looks pretty thin after this!

---
title: "screenR"
output: flexdashboard::flex_dashboard
runtime: shiny
---

```{r}
# include the module
source("screenrdata.R")
```

Charts
======

### Screening Scatter

```{r}

# call the module
xyUI("id1")
callModule(screenchart, "id1")
```



回答2:


You can have some UI code in a different file and then include it in your main UI with

source("file.R", local=TRUE)$value

You can see more details on this shiny article http://shiny.rstudio.com/articles/scoping.html



来源:https://stackoverflow.com/questions/30560405/split-big-shinydashboard-app-into-pieces

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