Keeping R Shiny code organized can be a challenge. One method to organize your Shiny UI and Server code is to use a combination of R’s list and source functions. Another method to organize you’re Shiny code is through modularization techniques. Here though, we’re going concentrate on the list and source options. If you feel comfortable with Shiny and want to get strait to the answer, check out the Solution section. Otherwise, read on! Outline Difficulty Organizing Shiny UIs A UI Source Example That Usually Doesn’t Work When Source Fails in the UI Solution Usage Note Organizing the Shiny Server with Source Summary Difficulty Organizing Shiny UIs If you have a Shiny application with a lot of controls, it’s easy to get lost in the braces, parentheses and brackets. Using R’s list() and source() functions, we can make UI code much more manageable by moving components out to other files. So how do you do it? To get us started, we are going to work with the starter app provided in R studio. Click File →New →Shiny Web App… . A dialogue box will pop up. My setup details are shown below in the R-Studio dialogue box. Choose a working directory appropriate for you and press Create. R-Studio will then show the code for the starter Shiny App. The demo code has 4 parts. Load the Shiny Library Define the UI Define the Server Run the app. We are going to start on the UI portion of the Shiny app shown in the screen capture below. This particular UI can be broken down into two parts the “Side Bar Panel” [lines 9-13] and the “Main Panel” [lines 15-17]. What we are going to do is factor out the content in the Side Bar Panel to a file named Side_Panel_CTRL.R and the Main Panel content to a file named Main_Panel_OUTPUT.R. So, make the two new R scripts named Side_Panel_CTRL.R and Main_Panel_OUTPUT.R. From the app.R file, MOVE the sliderInput (lines 10-12 above) to the Side_Panel_Control.R file and the plotOutput (line 16) to the Main_Panel_OUTPUT.R. Make sure these two new files are saved in the same folder as your app.R file. A UI Source Example that Usually Doesn’t Work Once you have your files setup, add the source() statements into you app.R file as shown in the code snippet below (lines 8 &amp; 12). # Define UI for application that draws a histogram ui
The post Source and List: Organizing R Shiny Apps appeared first on R-BAR.