Managing Environments

Where we are

How do we keep this all running smoothly?


Some surprises are good


But this is production, not showbiz.

Improving & Protecting your Workflow

  • Manage your environment
  • Use a dev-test-prod workflow

Environments

Think of your data science workbench as a kitchen:

  • A repository is the grocery store, a central place where everyone gets their packages.
  • A library is the pantry, where you keep your own private set of packages.
  • A Package is the food!

Environments

👿 Sometimes things go wrong

  • Revisited an old R script, and it didn’t work.
    • “I swear that {insert function from package} worked before…”
  • Shared R code with a colleague, but they can’t get it to run.
    • “Why does this run on my computer, but not on Sue’s?”
    • “John pulled in my project from GitHub but can’t get it to work!”
  • You modified something in one project, and borked something in another project.
    • “I upgraded a package in one project, and now my other projects don’t work!”

Enter renv

The renv package helps you create reproducible environments for your R projects. Use renv to make your R projects more:

  • Isolated: Installing a new or updated package for one project won’t break your other projects, and vice versa. That’s because renv gives each project its own private package library.

  • Portable: Easily transport your projects from one computer to another, even across different platforms. renv makes it easy to install the packages your project depends on.

  • Reproducible: renv records the exact package versions you depend on, and ensures those exact versions are the ones that get installed wherever you go.

Enter renv

renv.lock

  • renv is designed to keep track of your PROJECT’S PACKAGE DEPENDENCIES
  • As your install, update, or delete packages from your project, you want to update your renv.lock file using renv::snapshot()
{
  "R": {
    "Version": "4.3.1",
    "Repositories": [
      {
        "Name": "RSPM",
        "URL": "https://packagemanager.posit.co/cran/latest"
      }
    ]
  },
  "Packages": {
    "BH": {
      "Package": "BH",
      "Version": "1.81.0-1",
      "Source": "Repository",
      "Repository": "RSPM",
      "Hash": "68122010f01c4dcfbe58ce7112f2433d"
    },
    "DBI": {
      "Package": "DBI",
      "Version": "1.1.3",
      "Source": "Repository",
      "Repository": "RSPM",
      "Requirements": [
        "R",
        "methods"
      ],
      "Hash": "b2866e62bab9378c3cc9476a1954226b"
    },
    "DT": {
      "Package": "DT",
      "Version": "0.28",
      "Source": "Repository",
      "Repository": "RSPM",
      "Requirements": [
        "crosstalk",
        "htmltools",
        "htmlwidgets",
        "jquerylib",
        "jsonlite",
        "magrittr",
        "promises"
      ],
      "Hash": "ab745834dfae7eaf71dd0b90f3b66759"
    }

renv.lock

  • renv is designed to keep track of your PROJECT’S PACKAGE DEPENDENCIES
  • As your install, update, or delete packages from your project, you want to update your renv.lock file using renv::snapshot()
  • Restore a project’s package dependencies using renv::restore()



Follow along with me - Getting started with renv

Dev-Test-Prod

Ensure your data products behave as expected and there are no surprises

Deployment Methods to Posit Connect

  1. Push-button
  2. Git-backed
  3. Programmatic

📣 Push-button is good for rapid prototyping. Beyond that, you really should be using git-backed or programmatic deployment via CI/CD pipeline to keep your code in sync with your deployment.

Git Backed Deployment

Connect will deploy directly from your git repository

Ref:

Dev-Test-Prod Methods // Git-backed Deployment

  • Deploy production and test branches

  • Automatically update production with a git merge

  • Stakeholders keep the same stable URL throughout

Dev-Test-Prod Methods // CI/CD Deployment

More Resources

That’s a wrap!
🤪 🥴 🫨 😵

Recap of Resources

https://posit-conf-2024.github.io/ds-workflows-r/resources.html

Now what?

Posit Team

Workshop Feedback

Please go to pos.it/conf-workshop-survey. Your feedback is crucial! Data from the survey informs curriculum and format decisions for future conf workshops, and we really appreciate you taking the time to provide it.