问题
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:
- 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:
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
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