Hans Geber
Hans Geber

Reputation: 193

Shiny App Bookmark Button Not Working in Docker Container

I'm running a Shiny app inside a Docker container, and I've encountered an issue where the bookmark button doesn't seem to function as expected. The app works perfectly outside of Docker, but when containerized, clicking the bookmark button doesn't trigger any visible action, and the expected bookmark state is not saved. I need to use enableBookmarking = "server" because I can't save all the data in the url.

app.R

library(shiny)

# Source the UI and server components
source("ui.R")
source("server.R")

# Run the application
shinyApp(ui = ui, server = server, enableBookmarking = "server")

ui.R

library(shiny)

ui <- fluidPage(
  # Title
  titlePanel("Simple Shiny App with Bookmarking"),
  
  # Text input
  textInput("text", "Enter some text:"),
  
  # Bookmark button
  bookmarkButton(),
  
  # Display the text input
  textOutput("display"),
  
  # Display the current bookmark directory
  textOutput("currentBookmarkDir")
)

server.R

library(shiny)

server <- function(input, output, session) {
  # Reactive value to store the current bookmark directory
  bookmarkDir <- reactiveVal()
  
  # Update the bookmark directory when a bookmark is created
  onBookmark(function(state){
    bookmarkDir(state$dir)
  })
  
  # Render the current bookmark directory
  output$currentBookmarkDir <- renderText({
    dir <- bookmarkDir()
    if (is.null(dir)) {
      "No bookmark created yet."
    } else {
      paste("Current bookmark directory:", dir)
    }
  })
  
  # Render the text input
  output$display <- renderText({
    paste("You entered:", input$text)
  })
}

Dockerfile

# Use the official R base image with Shiny
FROM rocker/shiny:4.3.1

# Install any additional R packages your app needs
# Uncomment and modify the following line if you need to install extra packages
# RUN R -e "install.packages(c('dplyr', 'ggplot2'))"

# Copy the app files into the Docker container
COPY app.R /srv/shiny-server/app.R
COPY ui.R /srv/shiny-server/ui.R
COPY server.R /srv/shiny-server/server.R

# Ensure that the Shiny server runs with the proper permissions
RUN chown -R shiny:shiny /srv/shiny-server

# Expose the Shiny server port (default is 3838)
EXPOSE 3838

# Run the Shiny app as a shiny user
USER shiny

# Start the Shiny server
CMD ["R", "-e", "shiny::runApp('/srv/shiny-server', port=3838, host='0.0.0.0')"]

Upvotes: 0

Views: 57

Answers (0)

Related Questions