##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/Raymond/Documents/R/win-library/4.1'
## (as 'lib' is unspecified)
## package 'shiny' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\Raymond\AppData\Local\Temp\RtmpSGToFt\downloaded_packages
install.packages("shinydashboard") # Install the shinydashboard package
## Installing package into 'C:/Users/Raymond/Documents/R/win-library/4.1'
## (as 'lib' is unspecified)
## package 'shinydashboard' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\Raymond\AppData\Local\Temp\RtmpSGToFt\downloaded_packages
install.packages("ggplot2") # Install ggplot2
## Installing package into 'C:/Users/Raymond/Documents/R/win-library/4.1'
## (as 'lib' is unspecified)
## package 'ggplot2' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\Raymond\AppData\Local\Temp\RtmpSGToFt\downloaded_packages
library("shiny")
library("shinydashboard")
##
## Attaching package: 'shinydashboard'
## The following object is masked from 'package:graphics':
##
## box
library("ggplot2")
Most updated COVID-19 cases in the United States. Data source (live updated) of COVID-19: https://raw.githubusercontent.com/nytimes/covid-19-data/master/live/us-states.csv
#Dataset of COVID-19 cases in the United States.
covid <- read.csv(url("https://raw.githubusercontent.com/nytimes/covid-19-data/master/live/us-states.csv"), stringsAsFactors = FALSE)
covid
Shiny apps are easy to write. No web development skills are required.
Figure: Shiny app of Google Trend Index
A Shiny app is a web page (UI) connected to a computer running a live R session (Server). Users can manipulate the UI, which will cause the server to update the UI’s displays (by running R code).
Shiny application consists of two major components:
Example of Shiny app: “R_SD_Lesson_2_template_00/app.R”. It is a shiny app we have discussed in lesson 1.
Figure: Example of Shiny app we have discussed in lesson 1
In this section, shinydashboard package will be used to build Shiny dashboards. I will demonstrate how to bilud an interactive dashboard with below steps. Demonstration file “R_SD_Lesson_2_demo_01/app.R”
Build a blank dashboard with Sidebar menu items and tabs
Add components with interactive map and dataframe to the dashboard
Figure: Interactive dashboard - interactive map
Figure: Interactive dashboard - dataframe
Create a dashboard similar to below one (Figure: Finalized dashboard outlook) by shinydashboard making use of Shiny. 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 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. Your coursework output should be similar to the figure “Finalized dashboard outlook”.
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). Please reuse the techniques in the lecture notes to complete your work.
# 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: Finalized dashboard outlook
Figure: Step 1, create the frame of dashboard
Figure: Step 2, add components into the frame
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. Reference links:
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/Raymond/Documents/R/win-library/4.1'
## (as 'lib' is unspecified)
## package 'rsconnect' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\Raymond\AppData\Local\Temp\RtmpSGToFt\downloaded_packages
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_template_02',account="r-shinydashboard-lesson-2")
## Preparing to deploy application...DONE
## Uploading bundle for application: 5706849...DONE
## Deploying bundle: 5608819 for application: 5706849 ...
## Waiting for task: 1105770773
## building: Building image: 6520573
## building: Fetching packages
## building: Installing packages
## building: Installing files
## building: Pushing image: 6520573
## deploying: Starting instances
## rollforward: Activating new instances
## terminating: Stopping old instances
## Application successfully deployed to https://r-shinydashboard-lesson-2.shinyapps.io/R_SD_Lesson_2_template_02/
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