Home > Net >  The difference between the structures of shiny apps for local and server deployment
The difference between the structures of shiny apps for local and server deployment

Time:08-05

I've made some shiny apps that work well when run locally, but not when deployed online on a shiny server (placed in /srv/shiny-server/). Here are the contents of a simple shiny_test.R file as an example. When deployed online it just displays an index (see image).

enter image description here

library(shiny)

###################################################
# ui
###################################################
# Define UI for app that draws a histogram ----
ui <- fluidPage(
  
  # App title ----
  titlePanel("Hello Shiny!"),
  
  # Sidebar layout with input and output definitions ----
  sidebarLayout(
    
    # Sidebar panel for inputs ----
    sidebarPanel(
      
      # Input: Slider for the number of bins ----
      sliderInput(inputId = "bins",
                  label = "Number of bins:",
                  min = 1,
                  max = 50,
                  value = 30)
      
    ),
    
    # Main panel for displaying outputs ----
    mainPanel(
      
      # Output: Histogram ----
      plotOutput(outputId = "distPlot")
      
    )
  )
)

###################################################
# server
###################################################
# 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 = "#75AADB", border = "white",
         xlab = "Waiting time to next eruption (in mins)",
         main = "Histogram of waiting times")
    
  })
  
}

###################################################
# Run app
###################################################
# runApp("my_app")
shinyApp(ui = ui, server = server)

I've seen that historically shiny apps used to have two files - server.R and ui.R, and wondered whether that would solve it, but I'm not sure what the rules are?

CodePudding user response:

Unfortunately this is not the time to be creative with file names. Rename shiny_test.R to app.R and try again. Proof:

Download shiny server docker image so that I don't have to install it on my system.

➜ docker pull rocker/shiny
Using default tag: latest
latest: Pulling from rocker/shiny
d7bfe07ed847: Pull complete 
896c02edbac0: Pull complete 
5297d3569d0f: Pull complete 
267bd2730782: Pull complete 
c9a275481623: Pull complete 
e82cec2b1844: Pull complete 
Digest: sha256:bc29f76ebe1f1e32d798cadff9e383fecfab9e9f16bf76148c7ed75dbf9965ee
Status: Downloaded newer image for rocker/shiny:latest
docker.io/rocker/shiny:latest

Run the broken app with shiny_test.R and the result.

➜ docker run --rm -p 3838:3838 -v /.../test:/srv/shiny-server/test rocker/shiny
[s6-init] making user provided files available at /var/run/s6/etc...exited 0.
[s6-init] ensuring user provided files have correct perms...exited 0.
[fix-attrs.d] applying ownership & permissions fixes...
[fix-attrs.d] done.
[cont-init.d] executing container initialization scripts...
[cont-init.d] 01_set_env: executing... 
skipping /var/run/s6/container_environment/HOME
[cont-init.d] 01_set_env: exited 0.
[cont-init.d] done.
[services.d] starting services
[services.d] done.

enter image description here

And then rename the R script and run again.

➜ mv shiny_test.R app.R
➜ docker run --rm -p 3838:3838 -v /.../test:/srv/shiny-server/test rocker/shiny
[s6-init] making user provided files available at /var/run/s6/etc...exited 0.
[s6-init] ensuring user provided files have correct perms...exited 0.
[fix-attrs.d] applying ownership & permissions fixes...
[fix-attrs.d] done.
[cont-init.d] executing container initialization scripts...
[cont-init.d] 01_set_env: executing... 
skipping /var/run/s6/container_environment/HOME
[cont-init.d] 01_set_env: exited 0.
[cont-init.d] done.
[services.d] starting services
[services.d] done.

enter image description here

  • Related