From 863277b2a0e9087994f687672865350f9731800b Mon Sep 17 00:00:00 2001 From: Peter Solymos Date: Sat, 6 Jul 2024 13:33:34 -0600 Subject: [PATCH] Add rhiny version Signed-off-by: Peter Solymos --- .github/workflows/r-rhino.yml | 58 +++++++++++++++++++++++ r-rhino/Dockerfile | 29 ++++++++++++ r-rhino/README.md | 32 +++++++++++++ r-rhino/rhino-app/app.R | 2 + r-rhino/rhino-app/app/main.R | 49 +++++++++++++++++++ r-rhino/rhino-app/app/static/favicon.ico | Bin 0 -> 318 bytes r-rhino/rhino-app/config.yml | 3 ++ r-rhino/rhino-app/dependencies.R | 2 + r-rhino/rhino-app/rhino.yml | 1 + 9 files changed, 176 insertions(+) create mode 100644 .github/workflows/r-rhino.yml create mode 100644 r-rhino/Dockerfile create mode 100644 r-rhino/README.md create mode 100644 r-rhino/rhino-app/app.R create mode 100644 r-rhino/rhino-app/app/main.R create mode 100644 r-rhino/rhino-app/app/static/favicon.ico create mode 100644 r-rhino/rhino-app/config.yml create mode 100644 r-rhino/rhino-app/dependencies.R create mode 100644 r-rhino/rhino-app/rhino.yml diff --git a/.github/workflows/r-rhino.yml b/.github/workflows/r-rhino.yml new file mode 100644 index 0000000..333d4a5 --- /dev/null +++ b/.github/workflows/r-rhino.yml @@ -0,0 +1,58 @@ +name: Build and Push Docker Image for faithful/r-rhino + +on: + push: + branches: [ "main" ] + paths: [ "r-rhino/**"] + pull_request: + branches: [ "main" ] + paths: [ "r-rhino/**"] + +env: + SHINY_SETUP: r-rhino + +permissions: + packages: write + +jobs: + build-and-push-image: + runs-on: ubuntu-latest + steps: + + - name: 'Checkout GitHub Action' + uses: actions/checkout@v4 + + - name: 'Docker metadata' + id: meta + uses: docker/metadata-action@v5 + with: + labels: | + maintainer=Peter Solymos + org.opencontainers.image.title=Faithful + org.opencontainers.image.description=Old Faithful Shiny app + org.opencontainers.image.vendor=Hosting Shiny Book Project + images: | + ghcr.io/${{ github.repository }}/${{ env.SHINY_SETUP }} + tags: | + type=raw,value=latest,enable={{is_default_branch}} + type=semver,pattern={{version}} + type=semver,pattern={{major}}.{{minor}} + type=semver,pattern={{major}} + type=ref,event=branch + type=ref,event=pr + + - name: 'Login to GitHub Container Registry' + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{github.actor}} + password: ${{secrets.GITHUB_TOKEN}} + + - name: 'Build and push' + uses: docker/build-push-action@v6 + with: + context: ./${{ env.SHINY_SETUP }} + file: ./${{ env.SHINY_SETUP }}/Dockerfile + push: ${{ github.event_name != 'pull_request' }} + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} diff --git a/r-rhino/Dockerfile b/r-rhino/Dockerfile new file mode 100644 index 0000000..36a0458 --- /dev/null +++ b/r-rhino/Dockerfile @@ -0,0 +1,29 @@ +# Define the parent image +FROM rocker/r2u:24.04 + +# Install general dependencies +RUN R -q -e "install.packages('deps')" + +# Add non-root user and group +RUN groupadd app && useradd -g app app + +# Set working directory to home of the non-root user +WORKDIR /home/app + +# Copy Shiny app files +COPY rhino-app . + +# Install dependencies +RUN R -q -e "deps::install(ask=FALSE)" + +# Set owner for the home folder +RUN chown app:app -R /home/app + +# Set user to non-root +USER app + +# Expose port +EXPOSE 3838 + +# Set command to execute at runtime +CMD ["R", "-e", "shiny::runApp(host='0.0.0.0', port=3838)"] diff --git a/r-rhino/README.md b/r-rhino/README.md new file mode 100644 index 0000000..efce872 --- /dev/null +++ b/r-rhino/README.md @@ -0,0 +1,32 @@ +## R Shiny as a Rhino app + +```bash +cd r-rhino/rhino-app +R -q -e "shiny::runApp(port=8080)" +``` + +Pull and run Docker image: + +```bash +docker pull ghcr.io/h10y/faithful/r-rhino:latest +docker run -p 8080:3838 ghcr.io/h10y/faithful/r-rhino:latest +``` + +Containerized version: + +```bash +# Change directory +cd r-rhino + +# If on MacOS X, set this +export DOCKER_DEFAULT_PLATFORM=linux/amd64 + +# Specify tag +export TAG=faithful/r-rhino:latest + +# Build image +docker build -t $TAG . + +# Run image, visit http://localhost:8080 +docker run --rm -p 8080:3838 $TAG +``` diff --git a/r-rhino/rhino-app/app.R b/r-rhino/rhino-app/app.R new file mode 100644 index 0000000..9328819 --- /dev/null +++ b/r-rhino/rhino-app/app.R @@ -0,0 +1,2 @@ +# Rhino / shinyApp entrypoint. Do not edit. +rhino::app() diff --git a/r-rhino/rhino-app/app/main.R b/r-rhino/rhino-app/app/main.R new file mode 100644 index 0000000..a0eea6f --- /dev/null +++ b/r-rhino/rhino-app/app/main.R @@ -0,0 +1,49 @@ +box::use( + shiny[fixedPage, moduleServer, NS, plotOutput, sliderInput, renderPlot, h2], + graphics[hist, box], + datasets[faithful], +) + +x <- faithful$waiting + +#' @export +ui <- function(id) { + ns <- NS(id) + fixedPage( + title = "Old Faithful", + h2("Old Faithful"), + plotOutput(outputId = ns("histogram")), + sliderInput( + inputId = ns("n"), + label = "Number of bins:", + min = 1, + max = 50, + value = 25, + ticks = TRUE + ) + ) +} + +#' @export +server <- function(id) { + moduleServer(id, function(input, output, session) { + output$histogram <- renderPlot( + alt = "Histogram of waiting times", + { + hist( + x, + breaks = seq(min(x), max(x), + length.out = input$n + 1 + ), + freq = TRUE, + col = "#BB74DB", + border = "white", + main = "Histogram of waiting times", + xlab = "Waiting time to next eruption [mins]", + ylab = "Frequency" + ) + box() + } + ) + }) +} diff --git a/r-rhino/rhino-app/app/static/favicon.ico b/r-rhino/rhino-app/app/static/favicon.ico new file mode 100644 index 0000000000000000000000000000000000000000..ac81e1f5bc791c07a458e87e50cb544f6a3333e8 GIT binary patch literal 318 zcmZQzU<5(|0RbS%!l1#(z#zuJz@P!d0zj+)#2|4HXaJKC0wf0muXAoO@N#f6JeF31 z2~kW7q5(!IpFvPi0K{fs1R+6)GY~YyUPdI>F&l%$n3Ro`loWtWC?BGbf#Lsu1cr$< e02v2?#N$Sw+(95d48+eN>K+5tH^>9!p!xuoG9P&W literal 0 HcmV?d00001 diff --git a/r-rhino/rhino-app/config.yml b/r-rhino/rhino-app/config.yml new file mode 100644 index 0000000..e829f27 --- /dev/null +++ b/r-rhino/rhino-app/config.yml @@ -0,0 +1,3 @@ +default: + rhino_log_level: !expr Sys.getenv("RHINO_LOG_LEVEL", "INFO") + rhino_log_file: !expr Sys.getenv("RHINO_LOG_FILE", NA) diff --git a/r-rhino/rhino-app/dependencies.R b/r-rhino/rhino-app/dependencies.R new file mode 100644 index 0000000..34a5ddb --- /dev/null +++ b/r-rhino/rhino-app/dependencies.R @@ -0,0 +1,2 @@ +# This file allows packrat (used by rsconnect during deployment) to pick up dependencies. +library(rhino) diff --git a/r-rhino/rhino-app/rhino.yml b/r-rhino/rhino-app/rhino.yml new file mode 100644 index 0000000..fea1327 --- /dev/null +++ b/r-rhino/rhino-app/rhino.yml @@ -0,0 +1 @@ +sass: node