fredr_set_key(Sys.getenv("FRED_API_KEY"))Deploying & Sharing Your Project
Publishing Apps and Reports on Posit Connect Cloud
Packages: shiny, quarto, rsconnect, renv
Functions: rsconnect::writeManifest(), Sys.getenv(), renv::init(), renv::snapshot()
- Create a free GitHub account if you don’t have one
- Sign up for a free account on Posit Connect Cloud (you can sign in with GitHub)
- Install rsconnect (
install.packages("rsconnect")) - Install renv and familiarize yourself with its basic purpose and features (
install.packages("renv")) - Update RStudio to a recent version so that Posit Connect Cloud shows up as a publishing option
Overview
Now that you have your Shiny app or your Quarto report, how can you share it with others? The simplest and most logical thing to do is to publish it as a web page. After all, sharing your work on the web is sort of the point of making a web app, and doing so is essential if you want to share your project with people who do not know how to use R. There are many ways to publish, but the easiest when you are getting started is Posit Connect Cloud, Posit’s free hosting service for Shiny apps, Quarto documents and other data science content. (You may see older tutorials that use shinyapps.io; Connect Cloud is its successor, so skip those.)
The good news is that the steps are the same whether you are publishing a Shiny app or a Quarto report. The only differences are which file you tell Connect Cloud is your primary file and which content type you choose:
| Shiny app | Quarto report | |
|---|---|---|
| Primary file | app.R |
report.qmd (or whatever you named it) |
| Content type | Shiny | Quarto |
There are two ways to publish: directly from your IDE (RStudio or Positron), or from a GitHub repository. Publishing from your IDE is the quickest way to get started. Publishing from GitHub takes a little more setup but means your project republishes automatically every time you push a change.
For the final project, the requirement is that your app or report is published on Posit Connect Cloud and that you submit the link along with your zipped project folder on Blackboard. See Final Project for details. Sharing your code on GitHub is optional unless you choose the GitHub route for publishing.
Before you publish
A lot of deployment problems come from projects that work on your machine only because of something specific to your machine. Before you publish, check that:
-
Everything lives in one project folder: your
app.Ror.qmdfile plus any data files and helper scripts it needs. -
Your file paths are relative, e.g.
read_csv("data/my_data.csv")rather thanread_csv("/Users/me/Desktop/project/data/my_data.csv"). -
There is no
setwd()orinstall.packages()in your code. -
Every package is loaded with
library()near the top of the file. This is how Connect Cloud figures out what to install. - The project runs from a fresh R session. Restart R (Session > Restart R) and run or render it one more time.
Keep your API keys out of your code
If your project downloads data through an API that requires a key (like the FRED app in Advanced Shiny), do not publish the key in your code. This is especially important if you publish from GitHub, because the free plan requires a public repository that anyone can read. You have two options:
-
Download the data once and save it to a file. Run your API calls in a separate script, save the result with
write_csv()orsaveRDS(), and have your app or report read the file. This is usually the simplest approach, and it makes your project faster too. -
Store the key as a secret variable on Connect Cloud. When you publish, add a secret variable (e.g.,
FRED_API_KEY) with your key as the value. Then read it in your code withSys.getenv():
To make this work on your own computer, add a line like FRED_API_KEY=yourkeyhere to a file called .Renviron in your project folder (you can open it with usethis::edit_r_environ("project")) and restart R. Never commit .Renviron to GitHub—add it to your .gitignore.
Option 1: Publish from your IDE
RStudio
- Open your
app.Rfile. If you are publishing a report, open your.qmdfile and click Render first. - Click the blue Publish icon (a circle with arrows) at the top of the editor and, if prompted, install any required updates.
- Choose Posit Connect Cloud and log in to connect your account. You only need to do this the first time.
- Select the files to upload. Include the essentials—your main file, data files and helper scripts—but not ancillary files like your data prep script or
.Rprofile. - Click Publish.
When it finishes, your project will open in the browser at a URL that looks like https://yourname-project.share.connect.posit.cloud. To update your project later, save your changes (and re-render, for a report) and click Publish again.
Positron
Positron comes with the Posit Publisher extension built in.
- Click the Posit Publisher icon in the Activity Bar.
- Under Credentials, click +, select Posit Connect Cloud and authorize access in your browser. Check that the code in the browser matches the one in Positron.
- Add a deployment and select your
app.Ror.qmdfile as the entrypoint. - Confirm the Project Files to include and add any Secrets (such as API keys).
- Click Deploy Your Project.
Positron needs an renv.lock file to know which packages your project uses, so run renv::init() in the console before you deploy (see below). Keep the .posit folder that Publisher creates so that later deploys update the same project rather than creating a new one.
Option 2: Publish from GitHub
Step 1: Create a manifest
When you publish from GitHub, Connect Cloud needs a list of the packages (and package versions) your project uses. You create this with the writeManifest() function from rsconnect. From your project folder, run:
rsconnect::writeManifest()This creates a file called manifest.json. If you add a package to your project later, run writeManifest() again.
Step 2: Push your project to GitHub
Commit your main file, manifest.json, and any data files and helper scripts, then push them to a public GitHub repository. To create a new repo, go to your GitHub profile, select Repositories, click New and give the repository a name. Make sure it is set to Public, click “Create repository,” and then connect your project folder to it (in RStudio, the easiest way is to create a new Version Control project from the repo’s URL and copy your files into it).
Step 3: Publish on Connect Cloud
- Sign in to Posit Connect Cloud and click Publish.
- If this is your first time, install the GitHub App when prompted and give it access to your repository.
- Choose Shiny (for an app) or Quarto (for a report).
- Select your repository and branch (usually
main). - Select your primary file:
app.Ror your.qmdfile. - If your project needs an API key, open Advanced settings, click + Add variable, and enter the name (e.g.,
FRED_API_KEY) and value. - Leave Automatically publish on push on (recommended) and click Publish.
From now on, every time you push a change to main, Connect Cloud will republish your project. If you turned automatic publishing off, click Republish on your project’s page instead.
Troubleshooting
-
“There is no package called …”: Make sure every package is loaded with
library()and, if you publish from GitHub, re-runrsconnect::writeManifest()and push again. - “Cannot open file …”: You are using an absolute path, or you didn’t include the data file when you published.
- The project works locally but not online: Open your project’s page on Connect Cloud and check the logs. The error message there is usually the same one you would see in the R console.
-
An API call fails online: Your secret variable is missing or misnamed. Check that the name in Connect Cloud matches the name in
Sys.getenv()exactly.
Always open your published link in a private browser window to see what other people will see.
Using the renv package to record your R environment
One problem you will frequently run into is that projects can break as a result of package updates and new R installations. One way to prevent this from happening is to use the renv package, which records your R environment at the time you made the project.
To do this, make sure renv is installed. Then type renv::init() in your console. Next, type renv::snapshot(). At this point, renv will give you a list of the packages and versions being used by the project. If everything looks good, type ‘y’ and renv will record the environment in a file called renv.lock. When you come back to work on the project later, you can choose whether to update the packages, but if doing so breaks the project, you have the option of restoring the earlier environment by typing renv::restore(). As a bonus, Connect Cloud (and Positron’s Publisher) can read renv.lock to figure out which packages to install.
