How to Deploy an R Shiny App as a Hugging Face Space

Ifeanyi Idiaye
5 min readJun 30, 2023

--

In this post, I will show you how to easily deploy your R Shiny app as a Hugging Face Space.

On May 8, 2023, in a tweet by Posit PBC, it was announced that R and Python developers can now deploy both R and Python Shiny apps on Hugging Face Spaces using their pre-built Docker templates.

This was great news because it offers an alternative way to deploy an R Shiny app beside using shinyapps.io, whose free tier is limited to deploying just 5 Shiny apps.

Excitedly, I decided to give it a try. I had on my computer a Shiny app I had written many months ago, just stored up in the app’s folder in my documents.

It is a simple app that calls the Google trends API and returns a graph of the search trend over time. It allows you to select the source of the trend data (Web, YouTube, News, or Images) as well as the time frame over which you want to examine the data. You can check out the code on GitHub here.

Now, let me walk you through how I successfully deployed the app on Hugging Face as a Space. First, though, it is important to note that each Hugging Face Space on the free tier is limited to 16GB RAM, 2 CPU cores, and 50GB of disk space. Therefore, if you want to deploy a Shiny app that performs intense computation, you may want to rent a Space that offers you a GPU.

Now, the first step to deploying your R Shiny app as a Space is to create a Space on Hugging Face. For this, you will need a Hugging Face account. If you do not have a Hugging Face account already, click here to create an account.

Having created an account, you can then proceed to the Shiny on Spaces documentation. There, under the heading Shiny for R, you will see a button labeled “Deploy on HF Spaces”. Press that button, and you will be directed to a page that looks like this:

Interface for creating a Hugging Face Shiny Space

In the “Space name” field, enter the name you want to give your Space. You could, for instance, give it the name of the Shiny app you want to deploy. Leave everything else as is, and press “Create Space”.

It will begin to build your Space for you, which will take some minutes to finish. Once done, it will launch a default Shiny app that looks like this:

Now here comes the interesting part. Go to “Files”, highlighted with the blue square in the image below:

Open the `app.R` file, click “edit”, select all the code there and hit delete. Paste the code of your app, then scroll down and press “Commit changes to main”.

Replace the code in app.R with your own

This will rebuild and restart your Space, because Hugging Face Spaces stores your data inside a Git repository; so, any time you make a change in your code and commit that change, your Space will be rebuilt and restarted.

After your Space has been rebuilt and restarted, go back to “Files”, and open the Dockerfile. Click “edit”, and then add all the libraries you used in building your app like so:

Add all your app libraries in the Dockerfile

Press the “Commit changes to main” button and let your Space rebuild and restart. When that is done, you now have a functioning Shiny app running as a Hugging Face Space.

When building my trend-checker Shiny app Space, though, I did encounter a small problem. The app did load up but my plot was unable to be displayed. I was getting an error about not being able to generate the “Rplots.pdf” file. It turns out the problem was that I made the ggplot interactive using the ggplotly() function from the plotly package. Therefore, I edited the code and made the plot a static one rather than an interactive one, and I could then visualize search trends over time. This is what the deployed Shiny app Space looks like:

You can visit the Space here. If you do, however, figure out a way to successfully deploy a Shiny app with interactive visualization as a Space without any error, please share with me 😁.

One really cool thing about Hugging Face Spaces is that you can share your Space either using the link to the Space or you can embed the Space in your website as an iframe. To find the link to your Space, click the three vertical dots top right of your Space highlighted in the image below with a yellow square:

Click the three dots at the top right position of your Space

Then select “Embed this Space”. You will see two options, either to copy the iframe or the direct URL to the Space. Copy the direct URL and paste it in your browser, and watch your app run as a Hugging Face Space. You can also share the URL with your colleagues, friends, or on social media.

Congratulations! You have just successfully deployed an R Shiny app as a Hugging Face Space!

I hope you enjoyed reading this post.

Follow me on Medium: @ifeanyidiaye

Follow me on Twitter: @Ifeanyidiaye

--

--

Ifeanyi Idiaye

I am a data scientist who is passionate about AI and building AI-driven applications. I am also a software developer and writer for Dev Genius.