R
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Data Structures
-
Section 4: Data Manipulation
-
Section 5: Visualization and Statistics
-
Section 6: Advanced R
-
Section 7: Practical Projects
-
Section 8: Interview Practice
-
Section 9: More Practice Exercises
-
Section 10: Shiny Apps in Depth
-
Section 11: More Data Wrangling
-
Section 12: Tidyverse Deep Dive
-
Section 13: Statistical Modeling Deep Dive
-
Section 14: Machine Learning in R
-
Section 15: R Visualization Deep Dive
-
Section 16: R Package Development Deep Dive
-
Section 17: R for Reproducible Research
-
Section 18: R and Databases
-
Section 19: R Performance Optimization
-
Section 20: Bioinformatics and Specialized R
-
Section 21: More Shiny Practice
-
Section 22: More Practice Exercises
-
Section 23: R Data Structures Deep Dive
-
Section 24: More Interview and Review
-
Section 25: R for Business Analytics
-
Section 26: R Text Mining and NLP
-
Section 27: R Spatial Data Analysis
-
Section 28: R Deep Learning
-
Section 29: Advanced Statistical Techniques
-
Section 30: R Object Systems Deep Dive
-
Section 31: R Environments and Metaprogramming
-
Section 32: R for Finance
-
Section 33: R for Clinical and Health Data
-
Section 34: More Shiny Advanced Practice
-
Section 35: R Data Cleaning Deep Dive
-
Section 36: R Reporting Automation
-
Section 37: More Practical Projects Round 2
-
Section 38: R Ecosystem and Career
-
Section 39: More Visualization Practice
-
Section 40: R for Experimentation
-
Section 41: R for Genomics and Bioinformatics Deep Dive
-
Section 42: R for Marketing Analytics
-
Section 43: R Data Import/Export Deep Dive
-
Section 44: R String Processing Deep Dive
-
Section 45: R for Actuarial and Insurance Analytics
-
Section 46: R Testing and Quality Assurance Deep Dive
-
Section 47: R Version Control and Collaboration
-
Section 48: R Advanced Functional Programming
-
Section 49: R for Supply Chain and Operations
-
Section 50: More Practice Exercises Round 3
-
Section 51: R Dashboards and BI Integration
-
Section 52: R Data Governance and Ethics
-
Section 53: More Modeling Practice
-
Section 54: R Final Capstone Projects
-
Section 55: R for Sports Analytics
-
Section 56: More Interview Practice Round 2
-
Section 57: R Networking and APIs
-
Section 58: R for Environmental Science
-
Section 59: R Notebook and Documentation Practices
-
Section 60: More Data Wrangling Mastery
-
Section 61: R for A/B Testing at Scale
-
Section 62: R Package Ecosystem Deep Dive
4: Installing and Loading Packages
I've seen this happen to almost every R beginner—and honestly, even some veterans when they jump into a brand new project. You find a great piece of code online, you paste it into your console, and you get this:
ggplot(data = mtcars, aes(x = wt, y = mpg)) +
geom_point()
Error in ggplot(data = mtcars, aes(x = wt, y = mpg)) :
could not find function "ggplot"
It's frustrating because the code is syntactically perfect. There are no typos. The problem is that R is telling you, "I have no idea what a ggplot is." It's not that the function doesn't exist in the R ecosystem; it's just not currently in your "active" memory.
The difference between installing and loading
This is the part that trips people up. In R, there is a massive difference between installing a package and loading a package. I like to use a smartphone analogy: installing a package is like downloading an app from the App Store. You only do it once. Loading a package is like tapping the app icon to actually open it. You have to do that every single time you start a new R session.
If you try to run library(ggplot2) and get an error saying there is no package called ‘ggplot2’, it means you haven't "downloaded the app" yet. You'll need to run this first:
install.packages("ggplot2")
Note the quotation marks. install.packages() expects a string (the name of the package in quotes) because it's looking for that name on a remote server (CRAN). Once that's done, the package lives on your hard drive, but it's still just sitting there. To actually use the functions, you bring it into your current workspace:
library(ggplot2)
Notice that library() doesn't require quotes. You're referring to an object already on your system.
Managing your package dependencies
As you get deeper into R, you'll realize that packages often depend on other packages. When you install ggplot2, R is smart enough to automatically install dplyr, rlang, and a handful of others that it needs to function. You don't need to hunt those down manually.
One piece of advice: don't put install.packages() inside your scripts. If you share your code with a colleague and include the install command, R will try to re-install the package every time they run your script. That's a waste of time and can occasionally overwrite their preferred version of a package. Keep your installations in the console and your library() calls at the very top of your script.
Using a function without loading the whole library
Occasionally, you might only need one single function from a massive package and you don't want to clutter your workspace with hundreds of other functions. You can use the double colon operator :: to do this.
# This works even if you haven't called library(ggplot2)
ggplot2::ggplot(data = mtcars, aes(x = wt, y = mpg)) +
ggplot2::geom_point()
I usually only do this in production code or when two different packages have functions with the same name (a "namespace conflict"). For your day-to-day analysis, sticking with library() is much cleaner.
📋 Practical Task
Building a Correlation Plot with the 'ggpubr' Package
Your goal is to create a correlation plot using the ggpubr package, which provides a simplified wrapper for ggplot2. Follow these steps to ensure the environment is set up correctly:
- Check if you have the
ggpubrpackage installed. If not, install it. - Load the
ggpubrlibrary into your session. - Using the built-in
irisdataset, use the functionggscatter()to create a plot withx = Sepal.Lengthandy = Sepal.Width. - To verify you understand the difference between loading and calling, try to run the
ggscatter()function again after restarting your R session without callinglibrary(ggpubr). Observe the error, then fix it using the::operator.
There are no comments for now.