how to split ui.r code into multiple parts?

1,355 views
Skip to first unread message

Huidong TIAN

unread,
Nov 25, 2013, 2:39:13 AM11/25/13
to shiny-...@googlegroups.com
In shiny's tutorial: http://rstudio.github.io/shiny/tutorial/#scoping, it said "ui.R" code can be split into multiple-parts, can anyone give an example?

For example, I want to source the following part:

  div(class = "input",
      uiOutput("uiCancer"),
      uiOutput("uiColon_analysis"),
      uiOutput("uiColon_method"),
      uiOutput("uiColon_hospital"),
      uiOutput("uiColon_diagnosisYear"),
      uiOutput("uiColon_leastCase"),
      uiOutput("uiColon_survivalYear"),
      uiOutput("uiColon_average")
  ),



Thanks!

Vincent Nijs

unread,
Nov 25, 2013, 3:01:15 AM11/25/13
to shiny-...@googlegroups.com
You can have your renderUI code, along with reactives, etc. in different files and source those files from server.R. For example, I have a number of 'tools' in a separate directory and source those files with the command below.

flist_analysis <- sourceDirectory('tools/analysis', recursive = TRUE)

The code in your post could go into ui.R and the renderUI code in various files sourced through server.R

Stéphane Laurent

unread,
Nov 25, 2013, 3:50:56 AM11/25/13
to shiny-...@googlegroups.com
You can also put it in global.R :
UI1 <-   div(class = "input",
      uiOutput("uiCancer"),
      uiOutput("uiColon_analysis"),
      uiOutput("uiColon_method"),
      uiOutput("uiColon_hospital"),
      uiOutput("uiColon_diagnosisYear"),
      uiOutput("uiColon_leastCase"),
      uiOutput("uiColon_survivalYear"),
      uiOutput("uiColon_average")
  )
and simply type UI1 in ui.R.

I don't understand why you use class="input", you can simply do:
  UI1 <- list(
      uiOutput("uiCancer"),
      uiOutput("uiColon_analysis"),
      uiOutput("uiColon_method"),
      uiOutput("uiColon_hospital"),
      uiOutput("uiColon_diagnosisYear"),
      uiOutput("uiColon_leastCase"),
      uiOutput("uiColon_survivalYear"),
      uiOutput("uiColon_average")
  )

Thank you Vincent, I did'nt know the sourceDiectory() function.

Huidong TIAN

unread,
Nov 25, 2013, 9:48:14 AM11/25/13
to shiny-...@googlegroups.com
I tried the second method, it doesn't work. And the first one has no advantage than in ui.r.

Huidong TIAN

unread,
Nov 25, 2013, 2:58:50 PM11/25/13
to shiny-...@googlegroups.com
So, the code In my post still need be in ui.R. But I may have hundreds of such lines? No other solution ?

Stéphane Laurent

unread,
Nov 25, 2013, 3:56:28 PM11/25/13
to shiny-...@googlegroups.com
Aaahh, we did'nt understand the question, I think. 

You mean you want some kind of meta-programming (the code writes the code), to generate the code with something like that :
> mynames <- c("analysis","method","hospital","diagnosisYear","leastCase")
> cat(sapply(1:length(mynames), function(i){
+   paste0('uiOutput("uiColon_', mynames[i], '")')
+ }))
uiOutput("uiColon_analysis") uiOutput("uiColon_method") uiOutput("uiColon_hospital") uiOutput("uiColon_diagnosisYear") uiOutput("uiColon_leastCase")

Is it what you mean ? 

Vincent Nijs

unread,
Nov 25, 2013, 10:08:45 PM11/25/13
to shiny-...@googlegroups.com
Not sure what you are looking for but I put renderUI for a tool in a separate file and then, based on a user selection (i.e., tool), I have a function as below in server.R and       uiOutput("ui_finder") in ui.R. The different files get sourced as I mentioned in my earlier email.

output$ui_finder <- renderUI({
get(paste0('ui_',input$tool))()
})

Huidong TIAN

unread,
Nov 26, 2013, 3:11:40 AM11/26/13
to shiny-...@googlegroups.com
Hi, thanks for your reply.

My ui.R file looks like: 


shinyUI(bootstrapPage(
  # Add custom CSS
  tagList(
    tags$head(
      tags$title("Klin_Mammae"),
      tags$link(rel="stylesheet", type="text/css",href="style.css")
    )
  ),
  
  ## Login module;
  div(class = "login",
    uiOutput("uiLogin"),
    textOutput("summary")
  ),  
  
  # Database, Group, Subgroup and Year; 
  div(class = "input",
      uiOutput("uiCancer"),
      uiOutput("uiBreast_Analysis"),
      uiOutput("uiBreast_A1_percent"),
      uiOutput("uiBreast_A1_hospital"),
      uiOutput("uiBreast_A1_year"),
      uiOutput("uiBreast_A1_stacked"),
      uiOutput("uiBreast_A1_total"),
      uiOutput("uiBreast_A1_average"),
      uiOutput("uiBreast_A2_percent"),
      uiOutput("uiBreast_A2_hospital"),
      uiOutput("uiBreast_A2_year"),
      uiOutput("uiBreast_A2_stacked"),
      uiOutput("uiBreast_A2_age"),
      uiOutput("uiBreast_A2_interval"),
      uiOutput("uiBreast_A2_total"),
      uiOutput("uiBreast_A2_average"),
      uiOutput("uiBreast_A3_percent"),
      uiOutput("uiBreast_A3_hospital"),
      uiOutput("uiBreast_A3_year"),
      uiOutput("uiBreast_A3_stacked"),
      uiOutput("uiBreast_A3_na"),
      uiOutput("uiBreast_A3_total"),
      uiOutput("uiBreast_A3_average"),
      uiOutput("uiBreast_A4_hospital"),
      uiOutput("uiBreast_A4_seperately"),
      uiOutput("uiBreast_A4_age"),
      uiOutput("uiBreast_A4_year"),
      uiOutput("uiBreast_A4_stage"),
      uiOutput("uiBreast_A4_leastCase"),
      uiOutput("uiBreast_A4_average")
  ),
  
  # Output: figures and table;
  div(class = "output", 
      uiOutput("uiBreast_A1_output"), 
      uiOutput("uiBreast_A2_output"),
      uiOutput("uiBreast_A3_output"),
      uiOutput("uiBreast_A4_output")
  ),
  div(chartOutput(" ", lib = 'datatables'))
))

Currently, I just added two analysis, and in future I need to add 12 more, the ui.R code will get too long to manage. Here is my server.R:
# Server application; 
shinyServer(function(input, output, session) { 
  ## Login module;
  source("www/Login.R",  local = TRUE)
  
  ## Cancer types and analysis; 
  source("www/analysis.R",  local = TRUE)
  
  ## Breast;
  source("Breast/Breast_A1.R",  local = TRUE)
  source("Breast/Breast_A2.R",  local = TRUE)
  source("Breast/Breast_A3.R",  local = TRUE)
  source("Breast/Breast_A4.R",  local = TRUE)
  
}) 

File "Breast/Breast_A1.R" contains the "renderUI" and "renderPlot"  mentioned in ui.R. 

The server.R file looks concise, while the ui.R looks a messy. Is there a way I can write ui.R like: 

shinyUI(bootstrapPage(
  # Add custom CSS
  tagList(
    tags$head(
      tags$title("Klin_Mammae"),
      tags$link(rel="stylesheet", type="text/css",href="style.css")
    )
  ),
  
  ## Login module;
  div(class = "login",
    uiOutput("uiLogin"),
    textOutput("summary")
  ),  
  
  # Database, Group, Subgroup and Year; 
  div(class = "input",
      uiOutput("uiCancer"),
      uiOutput("uiBreast_Analysis"),
      source("uiBreast_A1")
      source("uiBreast_A2")
      source("uiBreast_A3")
      source("uiBreast_A4")
  ),
  
  # Output: figures and table;
  div(class = "output", 
      uiOutput("uiBreast_A1_output"), 
      uiOutput("uiBreast_A2_output"),
      uiOutput("uiBreast_A3_output"),
      uiOutput("uiBreast_A4_output")
  ),
  div(chartOutput(" ", lib = 'datatables'))
))

Stéphane Laurent

unread,
Nov 26, 2013, 3:35:25 AM11/26/13
to shiny-...@googlegroups.com
Does it help https://groups.google.com/d/topic/shiny-discuss/s68AJCFsvxQ/discussion ? (you don't need the dyanmic number of widgets but otherwise I think it should help)

Huidong TIAN

unread,
Nov 26, 2013, 4:13:29 AM11/26/13
to shiny-...@googlegroups.com
Great! This is what I am looking for!  Thanks a lot!!
Reply all
Reply to author
Forward
0 new messages