Understand R Libraries/Packages

This post is a static and abbreviated version of this interactive tutorial on using R for social data analytics.

What is a library/package? Think of R as an operating system (e.g., iOS, Windows) and a library/package as an app running on the system. Each library is designed to accomplish specific tasks. For example, the library ggplot2–which is a library we will use throughout the semester–is for visualizing data, and the library rtweet is used for collecting Twitter data.

Use install.packages() to install libraries. Use library(), or require() to load an installed library.

In the example below, we install a fun library called cowsay.

# install.packages("cowsay") 
#make sure the library name is wrapped by quotation. 

library(cowsay) #load the library, alternatively, you can use require(cowsay)

You can run the following in your RStudio and see what happens.

library(cowsay)
say("Hello! Welcome to COMM 497DB")

What’s cool about computer programming is that codes are highly customizable: You can add and change parameters within a R function (e.g., say()). For example, by adding by=(“cow”), you can build a talking cow instead of the default talking cat.

library(cowsay)
say("Try me. Hello! I am a cow.", by="cow")
Avatar
Weiai Wayne Xu
Associate Professor in Computational Communication

Related