

Shiny saves the server function until a new user arrives. This causes Shiny to execute the server function. Shiny will run the whole script the first time you call runApp. However, where you place them will determine how many times they are run (or re-run), which will in turn affect the performance of your app, since Shiny will run some sections your app.R script more often than others. Shiny will execute all of these commands if you place them in your app.R script. You can load the maps and mapproj packages in the normal way with library ( maps ) library ( mapproj ) counties <- readRDS ( "data/counties.rds" ) Since you saved counties.rds in a sub-directory (named data) of the directory that server.R is in, you can load it with. Since you saved helpers.R in the same directory as server.R, you can ask Shiny to load it with source ( "helpers.R" ) In other words, the directory that you save server.R in will become the working directory of your Shiny app. When Shiny runs the commands in server.R, it will treat all file paths as if they begin in the same directory as server.R. Both source and readRDS require a file path, and file paths do not behave the same way in a Shiny app as they do at the command line. You will need to ask Shiny to call the same functions before it uses percent_map in your app, but how you write these functions will change. We also ran library(maps) and library(mapproj). To use percent_map, we first ran helpers.R with the source function, and then loaded counties.rds with the readRDS function. Here it will plot the percent of white residents in the counties in the color dark green.

Percent_map plots the counties data as a choropleth map.

Along the way, you will build a sophisticated app that visualizes US Census data.Ĭounties.rds is a dataset of demographic data for each county in the United States, collected with the UScensus2010 R package.
Rcode setting working databas how to#
This lesson will show you how to load data, R Scripts, and packages to use in your Shiny apps.
