##Set default repo to fix the issue when publishing html
local({r <- getOption("repos")
       r["CRAN"] <- "http://cran.r-project.org" 
       options(repos=r)
})

2. Lesson 2


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

2.1 Create a simple Shiny app


  • 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:

    1. The first component is the user interface, or UI, which is a web page that shows the information. It is composed of HTML components that you design with Shiny functions. In term of shinydashboard package, they are header, sidebar and body.
    2. The Server is the second component. This is the location of the application logic. It should be included in the server function.
  • 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


2.2 Three ways to deploy a Shiny app into a web page


  • You can share your apps with two basic options:

    1. Share your Shiny app as R scripts. This method requests your users have R on their own computer and know how to use it.

    2. 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


2.2.1 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:

    1. Sign up a free Shinyapps.io account by visiting here: https://www.shinyapps.io/admin/#/signup. I have signed up below account for demonstration.

    2. 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:

        1. Install rsconnect package by install.packages(“rsconnect”). please refer to below chunk.

        2. 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”.

        3. 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).

        4. Now your Shiny app has been shared as a web page. Reference links:

        5. 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\RtmpE5931o\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: 5400477 for application: 5361575 ...
## Waiting for task: 1071212968
##   building: Processing bundle: 5400477
##   building: Building image: 6256012
##   building: Fetching packages
##   building: Installing packages
##   building: Installing files
##   building: Pushing image: 6256012
##   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


2.2.2 Coursework: Share your Shiny app as a web page by shinyapps.io


  • By following the procedure in the lecture notes, try to share your Shiny app (R_SD_Lesson_2_template_01/app.R) as a web page by shinyapps.io.

2.2.3 Shiny Server


  • 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.


2.2.4 Coursework: Share your Shiny app as a web page by Shiny Server



2.2.5 RStudio Connect


  • 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/


2.2.6 Coursework: Share your Shiny app as a web page by RStudio Connect



2.3 Easy web publishing for R from RStudio


  • To publish R markdown documents to the web from RStudio, you can use a free service by RPubs.

2.3.1 RPubs website


  • 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:

    1. Create an R Markdown document (*.Rmd file)

    2. Click “File” > “Knit Document”