Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add feature/instructions for additional R packages #9

Closed
brandonerose opened this issue Sep 23, 2023 · 11 comments · Fixed by #15
Closed

Add feature/instructions for additional R packages #9

brandonerose opened this issue Sep 23, 2023 · 11 comments · Fixed by #15

Comments

@brandonerose
Copy link

I see the python version is able to add required packages such as plotly. Ideally, I would like to be able to use CRAN and GitHub packages in this format. I would also like to see how to handle data objects in readme or vignette.

@brandonerose
Copy link
Author

So I found a way to add additional packages! It should be in the documentation as it will be a common question from people...

You can install many, but not all, R packages. The ones available are at https://repo.r-wasm.org/ and you can add them to your app.R script using the line webr::install("plotly"). Below is a full version that worked for me!

webr::install('plotly')
webr::install('DT')
library('shiny')
library('plotly')
library('DT')

ui <- fluidPage(
  titlePanel("Hello Shiny!"),
  sidebarLayout(
    sidebarPanel(
      sliderInput(inputId = "bins",
                  label = "Number of bins:",
                  min = 1,
                  max = 50,
                  value = 30)
    ),
    mainPanel(
      plotOutput(outputId = "distPlot"),
      DTOutput("table1"),
      plotlyOutput('fig')
    )
  )
)

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")
    })
  output$table1 <- renderDT({datatable(mtcars)})
  output$fig <- renderPlotly({
    Animals <- c("giraffes", "orangutans", "monkeys")
    SF_Zoo <- c(20, 14, 23)
    LA_Zoo <- c(12, 18, 29)
    data <- data.frame(Animals, SF_Zoo, LA_Zoo)
    fig <- plot_ly(data, x = ~Animals, y = ~SF_Zoo, type = 'bar', name = 'SF Zoo')
    fig <- fig %>% add_trace(y = ~LA_Zoo, name = 'LA Zoo')
    fig <- fig %>% layout(yaxis = list(title = 'Count'), barmode = 'group')
    fig
  })
}

shinyApp(ui = ui, server = server)

@schloerke
Copy link
Collaborator

Thank you for the reprex and approach!

@schloerke
Copy link
Collaborator

I've made #13 and #14 to explore how data is being loaded.

I'm going to alter the issue to only be about instructions for loading additional R packages.

@schloerke schloerke changed the title add feature/instructions for additional R packages and data objects to readme add feature/instructions for additional R packages Sep 25, 2023
@schloerke schloerke changed the title add feature/instructions for additional R packages Add feature/instructions for additional R packages Sep 25, 2023
@stla
Copy link
Contributor

stla commented Sep 27, 2023

Hello,

Thank you for this discussion, it saved me ^^

IMHO a point is not clear in the documentation. After reading it, I tried webr::install in the R console (that returns an error), I didn't understand one had to call webr::install in the Shiny app.

@schloerke schloerke reopened this Sep 27, 2023
@schloerke
Copy link
Collaborator

@stla I'm in the middle of a different PR that I do not want to exit from. Would you mind making a PR to the readme? (Thank you in advance!)

@stla
Copy link
Contributor

stla commented Sep 27, 2023

Ok - will do.

@stla
Copy link
Contributor

stla commented Sep 28, 2023

I think the webr::install way is a bit annoying: I have to comment these lines when trying the app locally. And I have to not forget to uncomment them before exporting.

There is surely a way to automatically handle that, something like (I don't know) if("install" %in% NAMESPACE("webr")), but it would be desirable to have a cleaner way (maybe a separate file to enter the required packages, I don't know).

@schloerke
Copy link
Collaborator

I am working on a PR for shinylive assets that will handle the webr::install() automatically for both exported apps and within quarto documents.

When that is merged, I'll remove the readme documentation suggesting to use webr::install() at the top of the app.

@stla
Copy link
Contributor

stla commented Sep 28, 2023

There's no wish to use possibly different package versions with WebR in the future? Like renv, that would be great. In this case a separate file would be more appropriate.

@schloerke
Copy link
Collaborator

@stla

From #16 (comment)

In principle I think it should be possible to pre-build a Wasm filesystem image containing an R library (defined by renv, or a package list, or similar) ready to pre-load into the environment for a given shinylive/webR app.

Yes, it'll behave similar to an renv snapshot.

Currently all packages are the latest. Using the above approach will pull the in the packages statically. 1. reducing start up time. 2. freezing the packages being loaded.

@schloerke
Copy link
Collaborator

Original bug fixed in posit-dev/shinylive#68

Reduced comment in #21 as webr::install(PKG) is no longer required. Instead, include a library(PKG) or require("pkg") statement somewhere in your Shiny app.

Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants