Programmatic deployment of thousands of documents onto Rstudio Connect

Hello!

I am trying to figure out how I can go about deploying quite a few documents onto Rtudio Connect. I am looking for a solution that does not require too much manual work. I want to be able to create a section on Rstudio Connect for these documents then I want to create a solution that will allow people to be able to upload similar documents to that area (The people who will be deploying these documents in future have no experience with R). These documents that I am referring to are static PDF files.
I'm not sure if you can assist me?

Hey @Zuki! This is a great question!!

I think this workflow should be usable for you:

  • If you are an admin (or have an admin do this), create a tag / tag hierarchy for this content
  • Every deployment to RStudio Connect requires a manifest.json. You can build this pretty easily for each document (in a temporary directory) with:
tmpdir <- tempfile("directory")
dir.create(tmpdir, recursive = TRUE)

pdf_path <- fs::file_copy("myexample.pdf", tmpdir)
rsconnect::writeManifest(appDir = tmpdir, appFiles = "myexample.pdf", appPrimaryDoc = "myexample.pdf")

fs::dir_ls(tmpdir)
  • Then you can deploy this programmatically using connectapi. This uses the RStudio Connect Server API, which is documented here: https://docs.rstudio.com/connect/api . There is a way to do this with rsconnect too, I believe, but I am less familiar with it:
library(connectapi)
client <- connect()

bnd <- bundle_dir(tmpdir)
myapp <- deploy(client, bnd, name = "uniquename", title = "My Title")

poll_task(myapp)

browse_dashboard(myapp)
  • Lastly, to enable other users to do this type of thing, you could build a simple Shiny app that does this type of deployment for you. You would just inject the API key necessary to do the deployment into the Shiny application's "Environment Variables"

The one piece I have left off here is programmatically setting the tag. There is not a great way to do this today, unfortunately. There is an experimental myapp$tag_set() function that uses the tag ID, but that is not super usable. I will have to do some thinking on that front and whether there is a better approach to doing so programmatically :smiley:

I hope that is helpful as a start though! I'm curious what your thoughts are on whether this will be workable in your context or not.

Hello

Sorry for the delayed response. I found another way to upload the pdf files onto Rstudio Connect

file <- list.files("directory", full.names = T) #directory with all files using option full.names = T

  for (i in file){
  
  rsconnect::deployDoc(doc=i,
     account = "admin number",                
     server = "server")
  
}

I have however come across a small problem, after I upload all the files onto Rstudio the format of the file names changes, eg:. "Tennis Score Totals - May 2020.pdf " will appear as "tennis_score_total-_may_2020.pdf" is there a reason for this? How do I ensure that the format of the file names does not change after I upload onto Rstudio?

1 Like

That file name changing is "sanitization" / "cleaning" of the filename to remove "bad" characters that are not presented well by browsers. That is something that the rsconnect package does, so there is not really a way out of that in the pattern you are using.

We have since added a helper to the connectapi package that could be helpful here,

library(connectapi)
client <- connect()
bnd <- bundle_static("my file.html")
myapp <- deploy(client, bnd)

set_vanity_url(myapp, "cole/example_space")

browse_dashboard(myapp)

This will preserve filenames, but unfortunately browsers do not render spaces well, so you end up with a URL like:

https://colorado.rstudio.com/rsc/cole/example_space/

which redirects to:

https://colorado.rstudio.com/rsc/cole/example_space/my%20file.html

Ultimately your easiest path is probably making your filenames browser friendly (i.e. remove spaces and dashes, lowercase, etc.)

1 Like