rstudio / pins-r

Pin, discover, and share resources
https://pins.rstudio.com
Other
312 stars 63 forks source link

board_s3, Shiny deployment, and paws dependency #673

Closed vgXhc closed 1 year ago

vgXhc commented 1 year ago

I just walked through this issue with shinyapps.io support, and I think this qualifies as a {{pins}} issue:

I have a Shiny app that uses board_s3 to read in data. Reprex (if you provide your own S3 credentials)

library(pins)
board <- board_s3("vzpins",
region = "us-east-1",
access_key = Sys.getenv("S3_ACCESS_KEY"),
secret_access_key = Sys.getenv("S3_SECRET_ACCESS_KEY"))

# Define UI for application that draws a histogram
ui <- fluidPage(

# Application title
titlePanel("Old Faithful Geyser Data"),

# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30)
),

# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
)

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

output$distPlot <- renderPlot({
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)

# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
}

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

The app worked fine locally, but after deploying it on shinyapps.io I got a (not very helpful) error:

022-10-19T15:19:22.316043+00:00 shinyapps[6862336]: Error in value[[3L]](cond) :
2022-10-19T15:19:22.316075+00:00 shinyapps[6862336]: Calls: local ... tryCatch -> tryCatchList -> tryCatchOne -> <Anonymous>
2022-10-19T15:19:22.316080+00:00 shinyapps[6862336]: Execution halted
2022-10-19T15:19:22.316096+00:00 shinyapps[6862336]: Shiny application exiting ...

Long story short, we ultimately identified the issue: When deploying the app with rsconnect(), only imported package dependencies are included in the bundle; suggested packages are not. board_s3 relies on paws.storage, which is apparently not a required package. Including library(paws.storage) in the code of the app made it work:

library(pins)
library(paws.storage)
board <- board_s3("vzpins",
region = "us-east-1",
access_key = Sys.getenv("S3_ACCESS_KEY"),
secret_access_key = Sys.getenv("S3_SECRET_ACCESS_KEY"))

# Define UI for application that draws a histogram
ui <- fluidPage(

# Application title
titlePanel("Old Faithful Geyser Data"),

# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30)
),

# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
)

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

output$distPlot <- renderPlot({
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)

# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
}

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

So maybe there's a way to explicitly include the paws.storage as required. Or at least a note about this issue somewhere in the documentation.

juliasilge commented 1 year ago

This is related to rstudio/vetiver-r#149, where I asked whether pins needs to have a concept of required_pkgs for boards, much like tidymodels model objects do:

library(parsnip)
rand_forest() %>% required_pkgs()
#> [1] "parsnip" "ranger"

Created on 2022-11-07 with reprex v2.0.2

This will help with building Dockerfiles and such, but I don't think would help with building an app that does the dependency sniffing the way that the rsconnect package does; that explicitly requires you to use library(paws.storage) or paws.storage::s3() for the dependency to be picked up. I think the best we can do for that case is document this better. Right now the documentation says this:

Pin data to a bucket on Amazon's S3 service, using the paws.storage package.

What do you think would have been more helpful to you here, to understand this board uses paws.storage?

vgXhc commented 1 year ago

Thanks for the response.

What do you think would have been more helpful to you here, to understand this board uses paws.storage?

Hm, good question. I guess I read "uses the paws.storage package" as "you need to have the package installed" and not as "you need to include library("paws.storage") in your code." I'm very used to R packages handling dependencies without me ever having to worry about them...

The fact that the code worked fine locally and the issue only appeared after deploying the app probably also led me down the wrong path in troubleshooting. (I don't have a good concept of what happens when I push the "publish app" button in RStudio). So I'm not really sure what language would be better. Is there maybe a way to create a more meaningful error message?

juliasilge commented 1 year ago

Thank you for your thoughts @vgXhc! 🙌

We are taking a couple of steps to improve this situation:

Thank you again for sharing what happened in your situation! 🙏

github-actions[bot] commented 1 year ago

This issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with a reprex: https://reprex.tidyverse.org) and link to this issue.