Quantcast
Channel: R-bloggers
Viewing all articles
Browse latest Browse all 12081

R Shiny Application Split Into Multiple Files

$
0
0

[This article was first published on Data Science Using R – FinderDing, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

An R Shiny application can be completely made of just one file that includes your UI and server code. Depending on the application you’re creating this can get messy as the number of lines grows. Splitting the app across multiple files also helps with debugging and being able to reuse code for another project.

For this post, the actual code and what it’s doing will not be covered. The code is more of a placeholder and this is creating a project framework to start Shiny applications.

Creating a new project

The best practice is to create a new project for each application. This is simple with RStudio and using the “File” dropdown to select “New Project”

Create a new r shiny project in rstudiocreate a new project in rstudioSelect “New Directory”create a Shiny Web Application in RStudioChoose “Shiny Web Application”create a shiny web application as a new projectGive your directory a name and the location you want to save it to.
example r shiny application

It’s a good idea to run the default Shiny app, if it doesn’t run then you may have installation issues or your security settings do not allow R shiny applications.

Now the code that is used in the example is all in one file, you can delete the file. We will be using 3 files/scripts,

  • ui.R
  • server.R
  • global.R – Not needed but helps to store functions and other code as an application grows large.

I will also create a new subdirectory called data

r shiny application directory structure

I will also create a new subdirectory called to store the data that will be used.

R Shiny Framework Below

UI Code

ui <- fluidPage(    titlePanel("R Shiny Framework"),    navlistPanel(        "Tabs",        tabPanel("First Tab",                 h3("Confirmed Flu Cases by Season"),                 selectInput("region_id", 'Please select a region',                              choices = unique(data$Region),                             selected = "WESTERN")                      ),        mainPanel(            plotOutput(outputId = "plot_id"),            h6("Data Provided by New York State Department of Health"))    ))

Server Script

server <- function(input, output, session) {    output$plot_id <- renderPlot({    temp <- filter(data, Region == input$region_id)    plot(x = temp$Season, y = temp$Count, xlab = "Season", ylab = "# Confirmed Flu Cases")  })}

Global Script

library(shiny)library(tidyverse)# Data Provided by New York State Department of Health# https://health.data.ny.gov/Health/Influenza-Laboratory-Confirmed-Cases-By-County-Beg/jr8b-6gh6data <- read.csv("./Data/Influenza_Laboratory-Confirmed_Cases_By_County__Beginning_2009-10_Season.csv")

End Results

R Shiny Framework application

var vglnk = { key: '949efb41171ac6ec1bf7f206d57e90b8' }; (function(d, t) {var s = d.createElement(t); s.type = 'text/javascript'; s.async = true;s.src = '//cdn.viglink.com/api/vglnk.js';var r = d.getElementsByTagName(t)[0]; r.parentNode.insertBefore(s, r); }(document, 'script'));

To leave a comment for the author, please follow the link and comment on their blog: Data Science Using R – FinderDing.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.


Viewing all articles
Browse latest Browse all 12081

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>