##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("ggplot2") # Install ggplot2
library("ggplot2")
COVID-19 cases as of 25 Oct 2021 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("R_SD_Data_covid.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_01/app.R”. It is a shiny app we have discussed in lesson 1.
Figure: Example of Shiny app we have discussed in lesson 1
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 method 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
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/
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”.
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.
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).
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. 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)
## package 'rsconnect' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\itray\AppData\Local\Temp\RtmpGm6oPD\downloaded_packages
library("rsconnect")
# For step 3. Deploy your application with shinyapps.io
rsconnect::deployApp('R_SD_Lesson_2_template_01')
## Preparing to deploy application...DONE
## Uploading bundle for application: 5361575...DONE
## Deploying bundle: 5400569 for application: 5361575 ...
## Waiting for task: 1071224154
## building: Processing bundle: 5400569
## building: Building image: 6256108
## building: Fetching packages
## building: Installing packages
## building: Installing files
## building: Pushing image: 6256108
## 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_01/
Figure: Pick an account name to proceed
Figure: On-screen instructions
Figure: Administration of your Shiny apps
Shiny Server is a companion program to Shiny that builds a web server designed to host Shiny apps. It’s free, open source, and available from GitHub.
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 explicit support for Ubuntu 12.04 or greater (64 bit) and CentOS/RHEL 5 (64 bit). If you are not using an explicitly supported distribution, you can still use Shiny Server by building it from source.
You can host multiple Shiny applications on multiple web pages with the same Shiny Server, and you can deploy the apps from behind a firewall.
With RStudio Connect, you can publish Shiny applications from the RStudio IDE with the push of a button.
You will need to install at least version 1.0.44 of the RStudio IDE to interact with 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.
Create RStudio Connect account: https://beta.rstudioconnect.com/connect/
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. Reference link: https://rpubs.com/raymondpolyu/850150