The first thing that you need to accomplish when using the
connectapi
is getting connected to your RStudio Connect
server. This requires the URL to your server and an API key. Directions
on how to get an API key are here.
Once you have your API key, you can create a Connect object in your code, like so:
library(connectapi)
<- connect(
client server = "http://example.com:3939",
api_key = "aihgaahegiahgg"
)
The alternative is to define a .Renviron
file, which
specifies environment variables:
CONNECT_SERVER=http://example.com:3939
CONNECT_API_KEY=aihgaahegiahgg
Then, those can be used in your code:
library(connectapi)
# this happens by default if you restart your R session
readRenviron(".Renviron")
<- connect() client
The connectapi
package has tools to help
programmatically deploy and manage content. To get started, you need to
use the rsconnect
package, specifically the
rsconnect::writeManifest()
function, to build a
manifest.json
file for a directory of content that you want
to deploy.
Once the manifest.json
file is present, you can
reference the directory and deploy it directly:
<- bundle_dir("./my/directory")
bnd
# name must be unique on the server
# if you do not specify it, a random value will be provided
<- client %>%
content_1 deploy(bnd, name = "my-content", title = "Amazing Report!!")
The content object (content_1
) includes information
about the deployment that you just requested. This can be explored
with:
%>% poll_task() content_1
Alternatively, you can immediately begin altering the settings of the content object while you wait for deployment to complete:
%>%
content_1 set_image_url("https://gph.is/29vyb0s") %>%
set_vanity_url("/my_clever_content")
# ensure the vanity URL is set as expected
%>%
content_1 get_vanity_url()
If you have content that already exists and you want to update it, you can do so using the content GUID (which you can find within RStudio Connect in the Info pane).
<- client %>%
content_2 deploy(bnd, guid = "d78ba9f8-bb57-422e-b164-9ecd8e4c4fd6") %>%
poll_task()
You can also use the RStudio Connect content GUID to retrieve information about existing content (if you want to examine or change settings)
<- client %>%
content_3 content_item(guid = "96532fbc-725e-441b-9cc6-a5622535241b")