Checking package versions is critical for reproducibility and debugging. When code breaks after updating packages, the first question is always “what version am I running?” I’ve spent countless hours tracking down issues that turned out to be version-related, which is why I always document which versions I’m using.

The packageVersion() function retrieves the version of an installed and loaded package. This is essential when you’re troubleshooting code that might behave differently across versions.

When to Check Package Versions

You’ll need package versions when:

  • Documenting dependencies for reproducibility
  • Debugging behavior changes after updates
  • Checking if your code is compatible with installed version
  • Reporting issues with specific package versions
  • Setting up reproducible environments

Methods to Check Versions

Method 1: Single package with packageVersion()

packageVersion("ggplot2")

Method 2: Check package details with packageDescription()

packageDescription("ggplot2")$Version

Method 3: List all loaded packages with sessionInfo()

sessionInfo()  # Shows R version + all loaded packages

Using packageVersion() Function

Let’s see how we can use packageVersion() function to check version of package:

# Get version package
packageVersion("ggplot2")

Output:

[1] ‘3.4.4’

Here the output shows package version of ggplot2 package.

Common Mistakes to Avoid

Mistake 1: Checking version of unloaded package

# ❌ WRONG - Package must be loaded first
packageVersion("tidyverse")  # Error if tidyverse not loaded

# ✅ CORRECT - Load library first
library(tidyverse)
packageVersion("tidyverse")  # Now works

Mistake 2: Using wrong package name format

# ❌ WRONG - Package names are case-sensitive, must be lowercase
packageVersion("GGplot2")    # Error: package not found
packageVersion("GGPLOT2")    # Error: package not found

# ✅ CORRECT - Use exact package name
packageVersion("ggplot2")    # Works!

Mistake 3: Getting version without storing it

# ❌ INCONVENIENT - Version only prints, doesn't store
packageVersion("dplyr")

# ✅ BETTER - Store for later use or comparison
version_info <- packageVersion("dplyr")
print(version_info)

Mistake 4: Not checking R version alongside packages

# ❌ INCOMPLETE - Package versions depend on R version
packageVersion("ggplot2")  # 3.4.4, but on what R version?

# ✅ COMPLETE - Include R version for reproducibility
cat("R version:", R.version$version.string, "\n")
cat("ggplot2 version:", packageVersion("ggplot2"), "\n")

Pro Tips

  1. Use sessionInfo() for full reproducibility - Captures R version + all loaded packages
  2. Document versions in your code - Add a setup section that prints all versions
  3. Save package versions to a file - For reproducible environments:
    sink("session_info.txt")
    sessionInfo()
    sink()
    
  4. Check version compatibility before updating:
    packageVersion("package_name") < "0.10.0"  # Returns TRUE/FALSE
    

See Also