Creating an RShiny App ====================== Please refer to the `RStudio `__ page for detailed instructions on launching an RStudio instance on Notebooks Hub. Open RStudio ------------ There are many ways to create a new file in RStudio. It is best practice to create new files inside R projects for isolated, self-contained working environments. See details `here `__. .. figure:: ../../img/rstudio/rstudio-control-panel.png :alt: A Screenshot of the RStudio Control Panel A Screenshot of the RStudio Control Panel Create a New Shiny Project -------------------------- First, create a new project directory. In the Navigation Toolbar, select the **New Project** icon on the left or use the **Project Explorer** dropdown on the right. |A Screenshot of the RStudio New Project icon| |A Screenshot of the RStudio New Project Panel| .. |A Screenshot of the RStudio New Project icon| image:: ../../img/rstudio/rstudio-new-project-icon.png .. |A Screenshot of the RStudio New Project Panel| image:: ../../img/rstudio/rstudio-new-project-corner.png Follow the steps in the New Project Wizard. 1. Create project by selecting **New Project**. .. figure:: ../../img/rstudio/rstudio-new-project-dir.png :alt: A Screenshot of the RStudio New Project Wizard A Screenshot of the RStudio New Project Wizard 2. Select desired project type. In this case, choose **Shiny Application**. .. figure:: ../../img/rstudio/rstudio-new-project-shiny-1.png :alt: A Screenshot of the RStudio New Project Wizard A Screenshot of the RStudio New Project Wizard 3. Name your project directory (e.g., ``projectName``) and, if applicable, navigate to the desired parent directory (e.g., ``~/work``) in which to place your project. This will create a project folder (e.g., ``projectName``) containing two files: a Shiny app file (e.g., ``app.R``) and an R project file (e.g., ``projectName.Rproj``). *Note: Files saved to your personal* **work** *directory will persist across user sessions.* .. figure:: ../../img/rstudio/rstudio-new-project-shiny-2.png :alt: A Screenshot of the RStudio New Project Wizard A Screenshot of the RStudio New Project Wizard .. figure:: ../../img/rstudio/rstudio-file-explorer.png :alt: A Screenshot of the RStudio New Project Wizard A Screenshot of the RStudio New Project Wizard To close or switch projects, use the **Project Explorer** dropdown and select **Close Project**. .. figure:: ../../img/rstudio/rstudio-close-project.png :alt: A screenshot of the RStudio project panel to close project A screenshot of the RStudio project panel to close project Create New Shiny File(s) - alternative method --------------------------------------------- Alternatively, new Shiny files can be created without the Project Wizard. 1. Select **File** > **New File** > **Shiny Web App…**. For a quicker method, click the **New File** icon and select **Shiny Web App**. .. figure:: ../../img/rstudio/rstudio-file-dropdown.png :alt: A screenshot of the RStudio new file panel A screenshot of the RStudio new file panel 2. Name your application (e.g., ``appName``). This will generate a folder inside the selected parent directory (e.g., ``~/work``), similar to creating a project directory noted above. Then, select whether to create a single or multiple file(s) for your application. *Note: Files saved to your personal* **work** *directory will persist across user sessions.* - **Single File (app.R):** This option will create a single ``app.R`` file for your application that contains all necessary Shiny app components. This is a useful option when creating smaller applications. - **Multiple File (ui.R/server.R):** This option will create two files, ``ui.R`` and ``server.R``, for your application that separates Shiny app components. This is useful when creating more complex applications with lengthy code. .. figure:: ../../img/rstudio/rstudio-new-shiny-app-split.png :alt: A screenshot of the RStudio new shiny app modal A screenshot of the RStudio new shiny app modal .. figure:: ../../img/rstudio/rstudio-appName.png :alt: A screenshot of the RStudio with split app files A screenshot of the RStudio with split app files Install Shiny Package for R --------------------------- The built-in R modules within Notebooks Hub come pre-configured with the necessary R packages installed, including ``shiny``. In order to confirm ``shiny`` is installed, run ``system.file(package='shiny')`` inside the R Console. If the package is installed in the loaded environment (e.g., ``R-0.2.0``), the command will output the path where the package is located. .. code:: sos system.file(package='shiny') .. figure:: ../../img/rshiny/rshiny-console-check.png :alt: A screenshot of the R Console confirming installed package A screenshot of the R Console confirming installed package If the package does not exist, no path will be returned (i.e., ``""``). To install, run ``install.packages("shiny")`` inside the R console. .. code:: sos install.packages("shiny") Shiny Demos ----------- Shiny has 11 built-in examples for demonstration. PolusAI also provides simple demos in the `examples repo `__. Using the R Console: 1. Load the ``shiny`` package by running ``library(shiny)``. 2. The following examples can be run individually by entering ``runExample("")``. .. code:: sos library(shiny) .. code:: sos runExample("01_hello") # a histogram runExample("02_text") # tables and data frames runExample("03_reactivity") # a reactive expression runExample("04_mpg") # global variables runExample("05_sliders") # slider bars runExample("06_tabsets") # tabbed panels runExample("07_widgets") # help text and submit buttons runExample("08_html") # Shiny app built from HTML runExample("09_upload") # file upload wizard runExample("10_download") # file download wizard runExample("11_timer") # an automated timer When running a Shiny app, the R Console will show an IP address indicating that the app is active and a **stop** icon in the top right corner of the console. .. figure:: ../../img/rshiny/rshiny-console-example.png :alt: A screenshot of the R Console loading Shiny library and running first demo example A screenshot of the R Console loading Shiny library and running first demo example The active Shiny app will launch in a new window. .. figure:: ../../img/rshiny/rshiny-example-01_hello.png :alt: A screenshot of RShiny demo example 01_hello A screenshot of RShiny demo example 01_hello Closing the app window should terminate the active run. This can be confirmed by checking the R console to see if the **stop** icon disappeared. If not, simply click the **stop** icon and the run will be terminated. Shiny App Components -------------------- | RShiny apps consist of three main components inside a single ``app.R`` script: | - user interface object | - server function | - call to ``shinyApp`` function (i.e., ``shinyApp(ui, server)``) Often, these can be separated into individual ``ui.R`` and ``server.R`` files, which is useful for apps with extensive code. Please visit Posit’s `Shiny Basics `__ for helpful lessons to learn more. .. code:: sos library(shiny) # Define UI ---- ui <- fluidPage( ... ) # Define server logic ---- server <- function(input, output) { ... } # Run the app ---- shinyApp(ui = ui, server = server) Run App ------- Apps can be run with ``runApp("dirName")`` if ``app.R`` lives in directory ``dirName/``.