Skip to content

Commit

Permalink
feat: serve a hello-world app
Browse files Browse the repository at this point in the history
  • Loading branch information
ciyer committed Jan 9, 2025
1 parent 1478fc5 commit 71a15ca
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 8 deletions.
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ COPY shiny-server.conf.tpl /shiny-server.conf.tpl
COPY docker-entrypoint.sh /docker-entrypoint.sh
RUN chown shiny:shiny /etc/shiny-server/shiny-server.conf

COPY src/r/app /home/shiny/app

USER shiny
ENTRYPOINT ["/bin/sh", "/docker-entrypoint.sh"]
CMD ["/init"]
51 changes: 45 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,57 @@
# Shiny Serving Demo

This is a demo project that serves a Shiny app from a Renku V2 project.
This is a demo project that shows how to serve a Shiny app from a Renku V2 project. It uses the [rocker shiny](https://rocker-project.org/images/versioned/shiny.html) Docker base image and makes some modifications necessary for compatibility with Renku sessions.

To use it, you can create a Renku V2 launcher that references the image URL in the project package. You need to also set the following settings:
You can see it in action at https://renkulab.io/v2/projects/cramakri/shiny-demo.

- user id: 999 (the `shiny` user in the Rocker image)
- port: 3838
## Serving your own shiny app

This project serves a "hello-world" Shiny example app. To serve your own app, you can clone this repo, add your app, and then configure a Renku V2 session launcher.

### Adding your app

Once you have cloned this repo, you can replace the code in the `src/r/app` folder with your app.

After doing that, you should build the image and test that the app works. This requires Docker installed on your machine, but it will help you debug problems quickly.

For example, you can build and run with the following commands:

```
docker buildx build -t renku/shiny-session --platform linux/amd64 .
docker run --rm -ti -e RENKU_BASE_URL_PATH="/test-app" -p 3838:3838 renku/shiny-session
```

If everything worked correctly, you should be able to connect a web browser to

http://localhost:3838/test-app/

And you should see your app there.


### Renku session launcher

Once your app runs locally, you can serve it from Renku. To do this, you will need to create a session launcher with the following pieces of information:

- the container image URL
- the port
- UID and GID for the container

This repo is configured to build a Docker image. You should see a section called **Packages** in the right-hand side of the page for this repo. Click there to get the URL for the image (you will probably want to most recent one).

It will look something like: `ghcr.io/swissdatasciencecenter/demo-shiny-serve:sha-1478fc5`

The other pieces of information are fixed:

- Port: 3838
- UID: 997 (the `shiny` user in the Rocker image)
- GID: 997

With that, you should have a session launcher that serves your Shiny app from Renku!

## References

This project builds on The Rocker Shiny which builds a Docker image containing the Shiny Server.
This project builds on The Rocker Shiny Docker image, which is designed to run the Shiny Server. Here are links to documentation for the Rocker image and Shiny Server. You may find them helpful for more advanced use cases.

- https://rocker-project.org/images/versioned/shiny.html
- https://posit.co/download/shiny-server/
- https://docs.posit.co/shiny-server/
- https://github.com/rstudio/shiny-server/
6 changes: 4 additions & 2 deletions shiny-server.conf.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ server {
# Renku will pass in the base URL path to the container
location $RENKU_BASE_URL_PATH/ {
# Host the directory of Shiny Apps stored in this directory
site_dir /srv/shiny-server;
# Host the directory of example Shiny Apps
# site_dir /srv/shiny-server;
# If you have assets that also need to be served, use site_dir
app_dir /home/shiny/app;
# Log all Shiny output to files in this directory
log_dir /var/log/shiny-server;
Expand Down
47 changes: 47 additions & 0 deletions src/r/app/app.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
library(shiny)
library(bslib)

# Define UI for app that draws a histogram ----
ui <- page_sidebar(
# App title ----
title = "Hello Shiny/Renku!",
# Sidebar panel for inputs ----
sidebar = sidebar(
# Input: Slider for the number of bins ----
sliderInput(
inputId = "bins",
label = "Number of bins:",
min = 1,
max = 50,
value = 30
)
),
# Output: Histogram ----
plotOutput(outputId = "distPlot")
)

# Define server logic required to draw a histogram ----
server <- function(input, output) {

# Histogram of the Old Faithful Geyser Data ----
# with requested number of bins
# This expression that generates a histogram is wrapped in a call
# to renderPlot to indicate that:
#
# 1. It is "reactive" and therefore should be automatically
# re-executed when inputs (input$bins) change
# 2. Its output type is a plot
output$distPlot <- renderPlot({

x <- faithful$waiting
bins <- seq(min(x), max(x), length.out = input$bins + 1)

hist(x, breaks = bins, col = "#007bc2", border = "white",
xlab = "Waiting time to next eruption (in mins)",
main = "Histogram of waiting times")

})

}

shinyApp(ui = ui, server = server)

0 comments on commit 71a15ca

Please sign in to comment.