mdneuzerling / lambdr

Run R containers on AWS Lambda
https://lambdr.mdneuzerling.com
Other
131 stars 12 forks source link

Running with R Shiny #21

Closed oesah closed 1 year ago

oesah commented 1 year ago

Hey everyone, first of all, thanks for this lib, it's awesome :)

We are currently trying to get R Shiny working with this lib. Unfortunately, we are stuck at No method asJSON S3 class: shiny.appobj.

Here is our runtime.R:

parity <- function() {
  require(shiny)
  library(lambdr)

  shinyApp(
    ui <- fluidPage(
      titlePanel("Hello Shiny!"),
      sidebarLayout(
        sidebarPanel(
          sliderInput(inputId = "bins",
                      label = "Number of bins:",
                      min = 1,
                      max = 50,
                      value = 30)

        ),
        mainPanel(
          plotOutput(outputId = "distPlot")

        )
      )
    ),
    server <- function(input, output) {
      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")

        })
    }
  )
}

lambdr::start_lambda()

Dockerfile:

FROM public.ecr.aws/lambda/provided

ENV R_VERSION=4.2.1
ENV ARCH=x86_64

RUN yum -y install wget git tar

RUN yum -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm \
    && wget https://cdn.rstudio.com/r/centos-7/pkgs/R-${R_VERSION}-1-1.${ARCH}.rpm \
    && yum -y install R-${R_VERSION}-1-1.${ARCH}.rpm \
    && rm R-${R_VERSION}-1-1.${ARCH}.rpm

ENV PATH="${PATH}:/opt/R/${R_VERSION}/bin/"

# System requirements for R packages
RUN yum -y install openssl-devel

RUN Rscript -e "install.packages(c('httr', 'jsonlite', 'logger', 'remotes', 'shiny'), repos = 'https://packagemanager.rstudio.com/all/__linux__/centos7/latest')"
RUN Rscript -e "remotes::install_github('mdneuzerling/lambdr')"

RUN mkdir /lambda
COPY runtime.R /lambda
RUN chmod 755 -R /lambda

RUN printf '#!/bin/sh\ncd /lambda\nRscript runtime.R' > /var/runtime/bootstrap \
    && chmod +x /var/runtime/bootstrap

ENTRYPOINT [ "/lambda-entrypoint.sh" ]

CMD ["parity"]

Anyone an idea how to get this working?

mdneuzerling commented 1 year ago

I never thought about using this to run Shiny, but I'd be keen to know if you can get it to work!

When R has finished running its calculations, the lambdr package _serialise_s the results into something that can be returned to the user. By default, it attempts to convert the result to JSON. You can read more about this and how to control the serialisation strategy in the lambda_config documentation.

This works just fine if you're returning a vector, list, or data frame. But in your case, the "result" of the function is an entire Shiny application. R has no idea how to convert this to a JSON, and so you get that error.

You need to think about what is being returned to the user when they invoke the Lambda. I'm not the most knowledgeable about Shiny's inner workings, but I believe that Lambda supports websockets, which Shiny requires.

oesah commented 1 year ago

It will not work, Shiny is highly stateful so Lambda won't work well.