hafen / trelliscopejs

TrelliscopeJS R Package
https://hafen.github.io/trelliscopejs
Other
262 stars 36 forks source link

renderTrelliscope() doesn't work in shinyapps.io #121

Open Ebj8 opened 1 year ago

Ebj8 commented 1 year ago

I have a basic shiny app using the gapminder data set. It works just fine when I render it locally, but when I host the app on shinyapps.io it gives me the following error: Couldn't load config: https://ebj8.shinyapps.io/Gapminder/_w_b0eb2e0d/trelliscopes/appfiles/config.jsonp?__loadTrscopeConfig__a177f523=jsonp_e4tdyzdgtas7oje

The logs have been largely unhelpful as hitting the "Render Trelliscope" button in my app doesn't appear to trigger anything in the logs.

Here is my app script:

library(shiny)
library(trelliscopejs)
library(gapminder)
library(ggplot2)
library(dplyr)
library(tidyr)

# This loads the gapminder dataset into 'dat'
dat <- gapminder |>
  filter(continent == "Europe")

ui <- fluidPage(
  titlePanel("Gapminder"),
  sidebarLayout(
    sidebarPanel(
      h3("Customize your trelliscope"),
      selectInput(inputId = "x_axis",
                  label = "x-axis variable",
                  choices = colnames(dat),
                  selected = "year"),
      selectInput(inputId = "y_axis",
                  label = "y-axis variable",
                  choices = colnames(dat),
                  selected = "lifeExp"),
      actionButton(inputId = "create_trelliscope",
                   label = "Render Trelliscope")
    ),
    mainPanel(
      # This function tells shiny where to put the trelliscope and how big to
      # make it
      trelliscopeOutput(outputId = "trelliscope", width = "80%", height = "1000px")
    )
  )
)

server <- function(input, output) {

  output$trelliscope <- renderTrelliscope({
    # The function below will be used for making plots in our trelliscope
    # Note: x represents a dataset that will be passed in
    scatter_plot = function(x) {
      ggplot(x, aes_string(input$x_axis, input$y_axis)) +
        geom_point() +
        theme_bw()
    }

    dat |>
      group_by(country) |>
      nest() |>
      mutate(
        # Use the scatter_plot function above to make a plot for each row of this
        # nested data set
        plots = map_plot(data, scatter_plot)
      ) |>
      ungroup() |>
      trelliscope(name = "Gapminder", nrow = 2, ncol = 4, path = "www/trelliscopes ")
  }) |>
    bindCache(input$x_axis, input$y_axis) |>
    bindEvent(input$create_trelliscope)

}

shinyApp(ui = ui, server = server)

And the app itself on shinyapps.io can be found at https://ebj8.shinyapps.io/Gapminder/

SJokubauskaite commented 1 year ago

Hi,

just change: trelliscope(name = "Gapminder", nrow = 2, ncol = 4, path = "www/trelliscopes ")

into: trelliscope(name = "Gapminder", nrow = 2, ncol = 4, path = "www/trelliscopes" , self_contained = TRUE)

Sadly this solution seems not to work with objects created with ggplotly.