Deploying to RStudio Connect: A Beginner's Guide

Estimated time:
time
min

<a href="https://appsilon.com/rstudio-connect-as-a-solution-for-remote-data-science-teams/" target="_blank" rel="noopener">Posit Connect (formerly RStudio Connect)</a> is a platform that simplifies the deployment of data science and analytics content for R and Python applications, such as Shiny apps, Plumber APIs, Flask APIs, R Markdown documents, and much more. It simplifies sharing your applications, models, data, packages and reports with your team in the organization. It also allows you to schedule your streamlined process for automated reports and data pipelines. There are many examples of use cases of Posit Connect. <blockquote>Ready to witness the brilliance of Shiny? <a href="https://explore.appsilon.com/shiny-demo-gallery" target="_blank" rel="noopener">Dive into our demo gallery for an immersive experience that will leave you inspired and informed</a>.</blockquote> Since Posit Connect allows for a vast amount of applications to be deployed and hosted, we will show in this detailed guide how to efficiently deploy your applications on Posit Connect using different interfaces: <ol><li>Using RStudio IDE</li><li>Using Python Jupyter</li><li>Directly from the terminal/script for R and Python applications</li></ol> <h3>Table of Contents</h3> <ul><li><strong><a href="#connecting-ide">Connecting our IDE to Posit Connect</a></strong></li><li><strong><a href="#publishing-applications">Publishing Your Applications</a></strong></li><li><strong><a href="#summary">Summary</a></strong></li></ul> <hr /> <h2 id="connecting-ide">Connecting our IDE to Posit Connect</h2> Before we start showcasing how to deploy on <a href="https://posit.co/products/enterprise/connect/" target="_blank" rel="noopener noreferrer">Posit Connect</a>, it is necessary to connect the IDE/environment to Posit Connect: <h3>RStudio IDE</h3> In order to connect on Posit connect from <a href="https://posit.co/download/rstudio-desktop/" target="_blank" rel="noopener noreferrer">RStudio IDE</a>, you must: <ul><li>Click on Tools and select Global options from the header menu.</li></ul> <img class=" wp-image-22573" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65aff99d3cd6cdfebfab6da9_global-options.webp" alt="" width="574" height="303" /> Step 1 <ul><li>Click on the Publishing tab and select “Connect…”.</li></ul> <img class=" wp-image-22575" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65aff99e406b0f2b66c5ee14_step2.webp" alt="" width="569" height="575" /> Step 2 <ul><li>Choose the option RStudio Connect.</li></ul> <img class=" wp-image-22577" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65aff99f9b485fa99397c29a_Step-3.webp" alt="" width="549" height="394" /> Step 3 <ul><li>Write the URL for your Posit Connect server.</li></ul> <img class=" wp-image-22579" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65aff9a051db347a12f0ecc1_Step-4.webp" alt="" width="524" height="387" /> Step 4 <ul><li>Authorize your IDE to connect to Posit Connect.</li></ul> <img class=" wp-image-22581" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65aff9a22186506e315b0d2d_Step-5.webp" alt="" width="587" height="239" /> Step 5 <strong>Now you’re able to publish to Posit Connect.</strong> <h4>API Keys</h4> For the remaining options of connecting to Posit Connect, you’ll require to have an API Key that is obtained from the Posit Connect Server: <ol><li>Log in to the Posit Connect Server.</li><li>Click on your username and select API Keys.</li><li>Create a new API Key<ol><li>Save the API Key in a safe location. This key will be shown only once!</li><li>If you lose this API Key, just delete it and create a new one.</li></ol> </li> </ol> <img class="size-full wp-image-22584" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65aff9a3b4ccc6248e232496_apikeys.webp" alt="" width="399" height="232" /> API Keys <h3>Connect to Posit Connect using R</h3> <ul><li>To connect with the Posit Connect server using R, you must first save the API Key on your environment for safety.</li></ul> <pre><code class="language-r"> usethis::edit_r_environ() </code></pre> <ul><li>It will prompt a <code>new .Renviron</code> file, for that you write the API Key.</li></ul> <pre><code class="language-r"> CONNECT_SERVER = "your_Posit.connect.URL" CONNECT_API_KEY = "yourapikey" </code></pre> <ul><li>Refresh the Rtudio instance</li><li>After this, you have to install the package <code>rsconnect</code>.</li></ul> <pre><code class="language-r"> install.packages("rsconnect") </code></pre> <ul><li>After, you can run the following code with the proper information for the Posit Connect Server, your username account, and the API Key.</li></ul> <pre><code class="language-r"> rsconnect::connectApiUser(  server = Sys.getenv("CONNECT_SERVER"),  account = "myusername",  apiKey = Sys.getenv("CONNECT_API_KEY") ) </code></pre> <ul><li><strong>Now you’re able to publish to Posit Connect.</strong></li></ul> <h3>Using Jupyter Notebook</h3> To establish a connection using the <a href="https://jupyter.org/" target="_blank" rel="noopener noreferrer">Jupyter Notebook</a>, firstly, you need to install the package <code>rsconnect-jupyter</code> for notebook extension: <ul><li>Installing in Python the package, the extension and enabling the extension.</li></ul> <pre><code> pip install jupyter rsconnect-jupyter jupyter nbextension install --sys-prefix --py rsconnect_jupyter jupyter nbextension enable --sys-prefix --py rsconnect_jupyter jupyter serverextension enable --sys-prefix --py rsconnect_jupyter </code></pre> <ul><li>Installing with <a href="https://www.anaconda.com/" target="_blank" rel="noopener noreferrer">Anaconda</a> using a new environment</li></ul> <pre><code> conda create --name rsconnect-jupyter conda activate rsconnect-jupyter conda install -c conda-forge jupyter rsconnect-jupyter </code></pre> <ul><li>After this, when you open the Jupyter Notebook, it will have the following icon:</li></ul> <img class="size-full wp-image-22586" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65aff9a478fab48ca15a075d_Jupyter-Notebook.webp" alt="" width="1172" height="246" /> Jupyter Notebook <ul><li>To connect, click on the icon and select <strong>Publish to Posit Connect</strong>.</li><li>Then set the information for the URL of Posit Connect server, the API Key and a nickname for the Posit Connect server for future reconnection.</li></ul> <img class="size-full wp-image-22588" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65aff9a550ab0e1953e7619b_Add-Posit-Connect-Server.webp" alt="" width="705" height="524" /> Add Posit Connect Server It will then prompt a new window to publish the Jupyter Notebook; you can close it. <strong>Now you’re able to publish to Posit Connect from a Jupyter Notebook.</strong> <h3>Python Terminal</h3> You can also connect using the Python terminal instead of the user interface of Jupyter Notebook. <pre><code> rsconnect add \    --server CONNECT_SERVER \    --name server.nickname \    --api-key CONNECT_API_KEY </code></pre> <h2 id="publishing-applications">Publishing Your Applications</h2> Now that you have set your connection to Posit Connect, publishing different types of applications is possible. In this segment, we’ll illustrate how you can do it, and you’ll be given examples of some application’s publishing processes. <h3>R &amp; RStudio IDE</h3> <h4>R Markdown/Quarto</h4> The first example will be for an R Markdown file: <pre><code> --- title: "Hello world" output: html_document params:    name:      label: "User name"      input: text      value: "username" --- <br>Hello, `r params$name`! </code></pre> This code example shows that we’re using parameters. This is important to illustrate that Posit Connect can rerun documents from parameter inputs. To deploy the document in RStudio Connect IDE, all we have to do is: <ul><li>Click on the publish button.</li><li>Publish to the connection set for the Posit Connect server.</li><li>Select <strong>publish document with server code</strong>.<ul><li>This is necessary if you want to change parameters or set a scheduled refresh.</li></ul> </li> </ul> <img class="size-full wp-image-22591" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65aff9a733fd73557f141f68_Click-Publish.webp" alt="" width="1537" height="835" /> Click Publish &nbsp; <img class="size-full wp-image-22593" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65aff9a832cb89e393b73ad5_Publish-to-RStudio-Connect.webp" alt="" width="590" height="390" /> Publish to RStudio Connect The document can be rendered again on Posit Connect without requiring any adjustment from R/RStudio IDE. It also allows for scheduling the document execution and sending emails. <img class="size-full wp-image-22595 alignnone" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65aff9a9a2a6a986d4550898_hello-world.webp" alt="" width="1103" height="436" /> <img class="size-full wp-image-22597 alignnone" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65aff9aace5fbbfd056e658d_scheduling.webp" alt="" width="319" height="758" /> You can also specify the Access: <img class="size-full wp-image-22600 alignnone" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65aff9ab17280680efc40d13_specify-acess.webp" alt="" width="1017" height="510" /> The same document can be deployed by using the R script: <pre><code class="language-r"> rsconnect::deployDoc(    "path/to/file.rmd",     appName = "simple_doc",     account = "account_name" ) </code></pre> <h4>Shiny App</h4> For this example, we’ll use the previous R Markdown code as the file <code>simple_doc.rmd</code> and create a Shiny app that downloads the document report and allows us to change the parameter from the app session. <pre><code class="language-r"> library(shiny) library(rmarkdown) <br>ui = fluidPage(  textInput("user", "Name"),  downloadButton("markdown", "Get report") ) <br>server &lt;- function(input, output, session) {    output$markdown &lt;- downloadHandler(    filename = "RMD_report.html",        content = function(file) {      params &lt;- list(name = input$user)            rmarkdown::render(        "./simple_doc.rmd",        output_file = file,        params = params,        envir = new.env(parent = globalenv())      )    }  ) } <br>shinyApp(ui, server) </code></pre> To deploy it on RStudio IDE is similar to the markdown approach: <ul><li>Click on <code>publish</code> button and <code>Publish Application…</code>.<ul><li>Remember also to choose the R Markdown file <code>simple_doc.rmd</code> as part of the published files.</li></ul> </li> </ul> <img class="size-full wp-image-22604 alignnone" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65aff9acee584c7bbe7e7f80_publish-app.webp" alt="" width="759" height="377" /> <ul><li>Select the shiny app file and publish it to the Posit Connect account.</li></ul> <img class="size-full wp-image-22606 alignnone" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65aff9acee584c7bbe7e7f80_publish-app.webp" alt="" width="759" height="377" /> <img class="wp-image-22608 size-full alignnone" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65aff9aeae0aeb94c65e5e6c_output.webp" alt="" width="400" height="185" /> It can also be deployed using the R script: <pre><code class="language-r"> rsconnect::deployApp(  appDir = "path/to/shiny_app/folder",  appName = "simple_app",  appFiles = c("app.r","simple_doc.rmd"),  account = "account_name" ) </code></pre> <h4>Plumber API</h4> For the API, we’ll make a simple code. <pre><code class="language-r"> #* Hello world back to user #* @param username The user name #* @get /echo function(username = "") {  paste0("Hello, ", username, "!") } </code></pre> To publish in RStudio IDE, we follow the same pattern as before. To publish in the R script we must run the code: <pre><code class="language-r"> rsconnect::deployAPI(    api = "path/to/your/folder_api" ) </code></pre> We can use the deployed API by: <ul><li>Get the URL of the hosted API.</li></ul> <img class="size-full wp-image-22611 alignnone" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65aff9b0cadba2bec8c621a4_hostedapi.webp" alt="" width="1546" height="455" /> <ul><li>Run the code with the parameters in the URL.<ul><li>Add the Authorization header to connect to Posit Connect server</li></ul> </li> </ul> <pre><code class="language-r"> library(httr) <br>response &lt;- GET(  url = "serverurl/echo?username=friend",  config = add_headers(    Authorization = paste("Key", Sys.getenv("CONNECT_API_KEY"))  ) ) <br>content(response) # [[1]] # [1] "Hello, friend!" </code></pre> <blockquote>Ready to transform your Jupyter Notebooks into stunning reports effortlessly? <a href="https://appsilon.com/quarto-and-jupyter-notebooks/" target="_blank" rel="noopener">Dive into the world of Quarto and unlock the potential of your data</a>.</blockquote> <h3>Python</h3> <h4>Jupyter Notebook</h4> To publish a Jupyter Notebook, all we have to do is click on the publish button and select the Posit connect sever that was set: <img class="aligncenter size-full wp-image-22602" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65aff9b0a0310cd414cb762f_jupyter-2.webp" alt="" width="834" height="233" /> To deploy it by the command line (CLI) just write: <pre><code class="language-r"> rsconnect deploy notebook \    --server CONNECT_SERVER  \    --api-key CONNECT_API_KEY \    your-notebook.ipynb </code></pre> If you wish to add additional files, just add them after the notebook reference, for further reference check <code>rsconnect-python</code>. <h4>Shiny App</h4> The process of deploying a shiny app (python) is quite similar: <pre><code class="language-r"> import matplotlib.pyplot as plt import numpy as np from shiny import App, render, ui <br>app_ui = ui.page_fluid(    ui.layout_sidebar(        ui.panel_sidebar(            ui.input_slider("n", "N", 0, 100, 20),        ),        ui.panel_main(            ui.output_plot("histogram"),        ),    ), ) <br>def server(input, output, session):    @output    @render.plot(alt="A histogram")    def histogram():        np.random.seed(19680801)        x = 100 + 15 * np.random.randn(437)        plt.hist(x, input.n(), density=True) <br>app = App(app_ui, server, debug=True) </code></pre> To deploy this app, all you have to do is run the <code>rsconnect-python</code> script: <pre><code class="language-r"> rsconnect deploy shiny -n CONNECT_SERVER path-to-shiny=app/ </code></pre> There are many other applications that can be hosted by <code>rsconnect-python</code>: <ul><li><code>api</code>: WSGI-compliant APIs (e.g., bottle, falcon, flask, flask-restx, flasgger, picnic).</li><li><code>flask</code>: Flask APIs (Note: flask is an alias of API.).</li><li><code>fastapi</code>: ASGI-compliant APIs (e.g, fastapi, quart, sanic, starlette)</li><li><code>dash</code>: Python Dash apps</li><li><code>streamlit</code>: Streamlit apps</li><li><code>bokeh</code>: Bokeh server apps</li></ul> <h2 id="summary">Summary</h2> In this blog post, we have shown how to efficiently deploy your applications on Posit Connect using different interfaces, with enough details and references to smooth your process of deploying your own application and collaborating with your colleagues. <blockquote>Did you find this useful? Learn more and level up your Shiny game. <a href="https://appsilon.us16.list-manage.com/subscribe?u=c042d7c0dbf57c5c6f8b54598&amp;id=870d5bfc05" target="_blank" rel="noopener">Subscribe to our Shiny Weekly Newsletter for updates, advocacy tips, and cool tutorials from our global Shiny community</a>.</blockquote> <h2>Resources</h2><ul><li><a href="https://appsilon.com/r-docker-getting-started/" target="_blank" rel="noopener">R Docker: How to Run Your R Scripts in a Docker Container</a></li><li><a href="https://appsilon.com/r-shiny-docker-getting-started/" target="_blank" rel="noopener">R Shiny Docker: How To Run Shiny Apps in a Docker Container</a></li><li><a href="https://appsilon.com/streamlit-tutorial-rstudio-connect/" target="_blank" rel="noopener">Streamlit Tutorial: How to Deploy Streamlit Apps on RStudio (Posit) Connect</a></li></ul>

Have questions or insights?

Engage with experts, share ideas and take your data journey to the next level!
Explore Possibilities

Share Your Data Goals with Us

From advanced analytics to platform development and pharma consulting, we craft solutions tailored to your needs.

Talk to our Experts
shiny
rstudio connect
deploy
posit
r community
rstudio