##Set default repo to fix the issue when publishing html
local({r <- getOption("repos")
r["CRAN"] <- "http://cran.r-project.org"
options(repos=r)
})
#
# install.packages("GlobalOptions")
# library(GlobalOptions)
# opt = set_opt(
# error=TRUE
# )
install.packages("shiny") # Install the Shiny package
## Installing package into 'C:/Users/itray/Documents/R/win-library/4.0'
## (as 'lib' is unspecified)
##
## There is a binary version available but the source version is later:
## binary source needs_compilation
## shiny 1.7.1 1.7.2 FALSE
## installing the source package 'shiny'
install.packages("shinydashboard") # Install the shinydashboard package
## Installing package into 'C:/Users/itray/Documents/R/win-library/4.0'
## (as 'lib' is unspecified)
## package 'shinydashboard' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\itray\AppData\Local\Temp\RtmpWiHAsJ\downloaded_packages
install.packages("ggplot2") # Install ggplot2
## Installing package into 'C:/Users/itray/Documents/R/win-library/4.0'
## (as 'lib' is unspecified)
##
## There is a binary version available but the source version is later:
## binary source needs_compilation
## ggplot2 3.3.5 3.3.6 FALSE
## installing the source package 'ggplot2'
library("shiny")
library("shinydashboard")
##
## Attaching package: 'shinydashboard'
## The following object is masked from 'package:graphics':
##
## box
library("ggplot2")
In this section, simple layout will be used to build dashboards. I will demonstrate how to bilud an interactive dashboard with below steps. Demonstration file “dashboard_02/app.R”
Build a blank dashboard with Sidebar multiple menu items and tabs
Add components with interactive map and dataframe to the dashboard
Figure: Build a blank dashboard with Sidebar multiple menu items and tabs
Figure: Add components - interactive map
Figure: Add components - dataframe
In this section, more complex layout will be used to build dashboards. The dashboard layout will be designed as multiple partitions and each partition will be added a component. Our objective is to build an analytical dashboard “Health care dashboard” that could indicate the overview of covid-19 with analysed data.
Create a dashboard similar to below one (Figure: Finalized dashboard appearance). Follow below steps to create the dashboard. Demonstration file is “dashboard_02/app.R”.
In step 1, you need to, by using mixed row and column layout, create the frame of dashboard first which is similar to figure “Step 1, create the frame of dashboard”. Build a blank dashboard with a header, sidebar, and body in the designed frame of the dashboard.
In step 2, you need to add below components into the frame. Refer to the figure “Step 2, add components into the frame”.
In step 3, you need to add content to components. Please use the below given dataset of COVID-19 cases in the United States. The final output of the dashboard is the figure “Finalized dashboard appearance”.
The dataset of COVID-19 cases in the United States is given below (live updated of COVID-19: https://raw.githubusercontent.com/nytimes/covid-19-data/master/live/us-states.csv).
# Given the dataset (Most updated COVID-19 cases in the United States) for plotting graph
covid <- read.csv(url("https://raw.githubusercontent.com/nytimes/covid-19-data/master/live/us-states.csv"), stringsAsFactors = FALSE)
Figure: Step 1, create the frame of dashboard
Figure: Step 2, add components into the frame
Figure: Finalized dashboard appearance (Health care dashboard that could indicate the overview of covid-19 with analysed data)
Create a dashboard similar to below one (Figure: Finalized dashboard appearance) by shinydashboard making use of Shiny. Your objective is to build a health care dashboard that could indicate the overview of total 4908 patients in the Stroke Prediction Dataset. You must follow below steps to complete your coursework.
In step 1, you need to, by using mixed row and column layout, create the frame of dashboard first which is similar to figure “Step 1, create the frame of dashboard”. Build a blank dashboard with a header, sidebar, and body in the designed frame of the dashboard.
In step 2, you need to add components into the frame. Refer to the figure “Step 2, add components into the frame”.
In step 3, you need to add content to the components. Please use the below given Stroke Prediction Dataset. Your coursework output should be similar to the figure “Finalized dashboard appearance”. Save your coursework as (R_SD_Lesson_2_coursework_01/app.R) which will be shared as a web page by shinyapps.io in next section.
Dataset: Stroke Prediction Dataset
Import the Stroke Prediction Dataset.
# Import the Stroke Prediction Dataset
df.healthcare <- read.csv(file = 'healthcare-dataset-stroke-data.csv', header = TRUE, stringsAsFactors = FALSE, strip.white = TRUE, sep = ",")
# patients who had a stroke
patient_stroke <- df.healthcare[df.healthcare$stroke == 1,]
# Number of stroke patients (Box 1)
nrow(patient_stroke)
## [1] 208
# Body mass index of stroke patients (Box 2)
mean(patient_stroke$bmi)
## [1] 30.47212
# Average glucose level in blood of stroke patients (Box 3)
mean(patient_stroke$avg_glucose_level)
## [1] 134.9176
# patients who had NOT a stroke
patient_no_stroke <- df.healthcare[df.healthcare$stroke == 0,]
# Number of non-stroke patients (Box 4)
nrow(patient_no_stroke)
## [1] 4700
# Body mass index of non-stroke patients (Box 5)
mean(patient_no_stroke$bmi)
## [1] 28.8173
# Average glucose level in blood of non-stroke patients (Box 6)
mean(patient_no_stroke$avg_glucose_level)
## [1] 103.9797
#box plot for Statistical summary of the age of patients who had a stroke (Box 7)
library(ggplot2)
ggplot(patient_stroke, aes(x=smoking_status, y=age, fill=smoking_status)) +
geom_boxplot(width=0.5,alpha=0.5) +
ggtitle("Statistical summary of the age of patients who had a stroke in various smoking status") +
labs(x = "Patients' various smoking status", y = "Age of patients") +
scale_y_continuous(breaks = seq(0,100,5))
#box plot for Statistical summary of the age of patients who had NOT a stroke (Box 8)
library(ggplot2)
ggplot(patient_no_stroke, aes(x=smoking_status, y=age, fill=smoking_status)) +
geom_boxplot(width=0.5,alpha=0.5) +
ggtitle("Statistical summary of the age of patients who had NOT a stroke in various smoking status") +
labs(x = "Patients' various smoking status", y = "Age of patients") +
scale_y_continuous(breaks = seq(0,100,5))
Figure: Step 1, create the frame of dashboard
Figure: Step 2, add components into the frame
Figure: Finalized dashboard appearance
You can share your apps with two basic options:
Share your Shiny app as R scripts. This method requests your users have R on their own computer and know how to use it.
Share your Shiny app as a web page. This is the most user friendly way to share a Shiny app. Your users can navigate to your app through the internet with a web browser. They will find your app fully rendered, up to date, and ready to go. In this workshop, this approach will be introduced and demonstrated. RStudio offers three ways to host your Shiny app as a web page, they are shinyapps.io, Shiny Server or RStudio Connect. Below figure shows you the comparison of RStudio Connect, Shiny Server and Shinyapps.io.
Figure: Compare RStudio Connect, Shiny Server and Shinyapps.io
In short, you can share your Shiny app as a web page in three ways:
Host it on shinyapps.io, which is a free cloud based service from RStudio.
Purchase RStudio Connect, which is a publishing platform for R.
Build your own Shiny Server, which is free and open source.
The easiest way to turn your Shiny app into a web page is to use shinyapps.io, RStudio’s hosting service for Shiny apps.
shinyapps.io lets you upload your app straight from your R session to a server hosted by RStudio. You have complete control over your app including server administration tools. You can find out more about shinyapps.io by visiting https://www.shinyapps.io/
shinyapps.io provides both free and paid accounts. Overview of free and paid accounts are shown in below figure “shinyapps.io free and paid accounts”. In the demonstration, free Shinyapps.io account will be used.
A free shinyapps.io(opens in a new tab) account for up to 5 shiny applications and 25 active hours per month.
Procedure to share your Shiny app as a web page by shinyapps.io:
Sign up a free Shinyapps.io account by visiting here: https://www.shinyapps.io/admin/#/signup. I have signed up below account for demonstration.
Follow the on-screen instructions for below steps:
Pick an account name to proceed. Account names can contain letters, numbers and hyphens, but can’t start with a hyphen or a number, and can’t end with a hyphen. Refer to figure: “Pick an account name to proceed”. You can also refer the pattern to my one created above.
Install the rsconnect R package to get started. The rsconnect package enables you to deploy and manage your Shiny applications directly from your R console. Follow below steps:
Install rsconnect package by install.packages(“rsconnect”). please refer to below chunk.
This is important step. The rsconnect package must be authorized to your account using a token and secret. Copy the token and secret and then just paste it into your R console to authorize your account. (Follow the on-screen instructions to do it). Refer to figure: “On-screen instructions”.
Once the rsconnect package has been configured, you’re ready to deploy your first application. Run the following code in your R console: rsconnect::deployApp(‘path/to/your/app’), place all your application files under the path. Please refer to below chunk for example. Remember enter “Y” in console to “update” your Shiny application (for updating only). Alternatively you can open the Shiny app file app.R directly and then click the blue publishing icon in the right top corner of the RStudio IDE, and then select the account to publish the Shiny app.
Now your Shiny app has been shared as a web page. Example link: https://r-shinydashboard-lesson-2.shinyapps.io/dashboard_01/
Login to your Shinyapps.io account for administration of your Shiny apps. https://www.rstudio.com/products/shinyapps/. Refer to figure: “Administration of your Shiny apps”.
# For step 1. Install rsconnect package
install.packages("rsconnect")
## Installing package into 'C:/Users/itray/Documents/R/win-library/4.0'
## (as 'lib' is unspecified)
##
## There is a binary version available but the source version is later:
## binary source needs_compilation
## rsconnect 0.8.25 0.8.27 FALSE
## installing the source package 'rsconnect'
library("rsconnect")
##
## Attaching package: 'rsconnect'
## The following object is masked from 'package:shiny':
##
## serverInfo
# For step 3. Deploy your application with shinyapps.io
rsconnect::deployApp('R_SD_Lesson_2_coursework_01',account="r-shinydashboard-lesson-2")
## Preparing to deploy application...DONE
## Uploading bundle for application: 7326014...DONE
## Deploying bundle: 6417583 for application: 7326014 ...
## Waiting for task: 1240335805
## building: Parsing manifest
## building: Building image: 7528698
## building: Installing system dependencies
## building: Fetching packages
## building: Installing packages
## building: Installing files
## building: Pushing image: 7528698
## deploying: Starting instances
## terminating: Stopping old instances
## Application successfully deployed to https://r-shinydashboard-lesson-2.shinyapps.io/r_sd_lesson_2_coursework_01/
Figure: Pick an account name to proceed
Figure: On-screen instructions
Figure: Administration of your Shiny apps
Figure: shinyapps.io free and paid accounts
RStudio Connect is a publishing platform for the work you create in R and Python. With RStudio Connect, you can share Shiny applications, R Markdown reports, Plumber APIs, dashboards, plots, Jupyter Notebooks, and more in one convenient place.
If you are looking for a full-meal deal for your data science project, RStudio Connect might be the best solution out there. If you are only looking for a way to host your Shiny apps, you might find other options cheaper and easier to maintain, such as shinyapps.io.
You will need to install at least version 1.0.44 of the RStudio IDE to interact with RStudio Connect. You can confirm this by opening the IDE and clicking “Help” > “About RStudio” and checking the version number at the top of that pane.
Procedure to publish your Shiny App to RStudio Connect: https://docs.rstudio.com/how-to-guides/pre-tasks/connect-account/
Install and configure RStudio Connect on a Linux system managed server (https://docs.rstudio.com/connect/admin/)
Ready your Shiny Web App and open the RStudio IDE
From the menu bar, select Tools > Global Options
Select the Publishing tab in the left sidebar and click Connect…
In the Connect Account window, select RStudio Connect.
Enter the public URL of the RStudio Connect Server and click Next. The public URL is the URL that you use to connect to RStudio Connect in your browser. https://docs.rstudio.com/connect/admin/appendix/configuration/#Server.Address
Log into RStudio Connect by linking your RStudio Connect account to the rsconnect R package
Verifying Account completed and click “Connect Account”
You’ve successfully connected your account and can now publish your Shiny App directly to RStudio Connect by clicking the blue publishing icon in the right top corner of the RStudio IDE.
Shiny Server is a companion program to Shiny that builds a web server designed to host Shiny apps. It is free and open source.
Shiny Server is a server program that Linux servers can run to host a Shiny app as a web page. To use Shiny Server, you’ll need a Linux server that has support for:
Ubuntu 16.04+
Red Hat/CentOS 6+
SUSE Linux Enterprise Server 12+.
Download Shiny Server link: https://www.rstudio.com/products/shiny/download-server/
Managing and configuring Shiny Server: https://docs.rstudio.com/shiny-server/
If you are not using an explicitly supported distribution, you can still use Shiny Server by building it from source. Building Shiny Server from Source (Instructions of prerequisites, installation and post-install): https://github.com/rstudio/shiny-server/wiki/Building-Shiny-Server-from-Source
Up to now, you have learnt the skill how to share your Shiny app as a web page by various methods.
In this section, you will learn how to publish R markdown documents to the web from RStudio by using RPubs.
RPubs is a free service that makes it easy to publish R markdown documents to the web from RStudio.
To publish to RPubs from RStudio, you simply create an R Markdown document then click the Publish button within the HTML Preview window.
Procedure to share your R Markdown document as a web page by RPubs:
Register a free RPubs account by visiting: https://rpubs.com/
Create an R Markdown document (*.Rmd file)
By clicking “File” > “Knit Document”, generate the HTML Preview window of the R Markdown document created in step 2.
In the HTML preview window, click the Publish button (in the right top corner)
Select publish to “RPubs” and click the Publish button
Write down the document details for your R Markdown document
Your R Markdown document will be published in the web. Example: https://rpubs.com/raymondpolyu/850491