3

I have started using R shiny recently and what I noticed is that code both in ui.R and server.R tend to become hard to read very quickly. This is mostly due to the nested calls to create the HTML structure, the switches to implement conditional panels etc...

Is there a tool that with which I can write lighter and more readable code that will produce the desired server.R and ui.R? I am thinking of something similar to CoffeeScript that compiles in Javascript or Jade that compiles in HTML. We've tried R markdown but we are not convinced.

Or maybe this can be easily achieved with some simple good practice. However, this seems hard to export some calls out of the shinyUI and shinyServer without breaking shiny reactiveness.

Any guidance would be greatly appreciated

eaglefreeman
  • 824
  • 2
  • 9
  • 20
  • I would be happy to see if someone has a tool which would simplify things. Normally I try to `source()` my files or Tabs once I know they are working 100%, those files will have loads of `tryCatch` so they don't break my app if something is wrong. Maybe you can adopt some sort of model designing your app, common ones in `C#` is `MVC - Model, View, Controller` – Pork Chop Feb 10 '16 at 14:51
  • +1 for `source()`, especially for non-Shiny functions in `server.R`, so what's left is mostly input and output. The one that drives me crazy is the very useful `renderUI`, because you end up with UI elements in `server.R`. – alistaire Feb 10 '16 at 14:59
  • Also, look into `partial` in `shiny` or look here http://stackoverflow.com/questions/27080089/how-to-organize-large-r-shiny-apps/27122115#27122115 – Pork Chop Feb 10 '16 at 15:32
  • you can also look into shiny `modules` – NicE Feb 10 '16 at 21:11

1 Answers1

1

For server.R

You could break your server up into smaller pieces and source those files as others have suggested, or you could modularize your code as described by RStudio here:

http://shiny.rstudio.com/articles/modules.html

For ui.R

Building the UI in R is great for small apps but as you suggest becomes a mess very quickly for anything substantial. I would recommend keeping the ui.R tight by building an html template and passing your UI variables to it as described here:

http://shiny.rstudio.com/articles/templates.html