hafen / trelliscopejs

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

how to render trelliscopejs in R shiny? #100

Open yanqingfu opened 3 years ago

yanqingfu commented 3 years ago

following code no output

library(shiny)
library(gapminder)
library(trelliscopejs)

ui <- fluidPage(

    titlePanel("Trelliscope Output Test"),

    fluidPage(

        mainPanel(

            strong(h3(helpText("Plotly Output"))),
            plotlyOutput("plot1",height = "400px"),
            strong(h3(helpText("Trelliscope Output"))),
            trelliscopeOutput("plot2", width = "100%", height = "400px")

        )
    )
)

server <- function(input, output) {

    output$plot1 <- renderPlotly({
        ggplot(gapminder,aes(x=year, y = lifeExp)) +
            geom_point()+
            facet_wrap(~ country + continent)
        ggplotly()
    })

    output$plot2 <- renderTrelliscope({
        ggplot(gapminder,aes(x=year, y = lifeExp)) +
            geom_point()+
            facet_trelliscope(~ country + continent,
                              nrow = 2, ncol = 7, width = 300, self_contained = TRUE)

    })
}

shinyApp(ui, server)
TenanATC commented 2 years ago

I might add that it would just be helpful to have a package Vignette that details the number of ways that Trelliscope objects can be embedded. It seems to imply that Shiny is one option but that there are a number of others via htmlWidgets, but this isn't spelled out. It would be helpful to all of us to see a couple of worked examples or explainers. The current Vignette's Embedding/Sharing section could be it's own document. Interesting package, so I'm looking forward to seeing how it matures!

janas-sebastien commented 1 year ago

I would have the same request. I'd like to get trelliscopejs working in my shiny apps, but I can't get it work. Here is another minimal example that gives a blank page.

library(shiny)
library(trelliscopejs)
library(ggplot2)
library(gapminder)

ui <- fluidPage(

    titlePanel("Trelliscope in shiny"),
    trelliscopeOutput("graph")
)

server <- function(input, output) {

    output$graph <- renderTrelliscope({
        qplot(year, lifeExp, data = gapminder) +
            xlim(1948, 2011) + ylim(10, 95) + theme_bw() +
            facet_trelliscope(~ country + continent, nrow = 2, ncol = 7, width = 300)
    })

}

shinyApp(ui = ui, server = server)
stla commented 1 year ago

This app works in Shiny, after creating a www subfolder:

library(trelliscopejs)
library(dplyr)
library(ggplot2)
library(tidyr)

ui <- shinyUI(fluidPage(
  trelliscopeOutput(outputId = "plot")
))

server <- shinyServer(function(input, output) {
  output$plot <- renderTrelliscope({

    mpg %>%
      nest(data = !one_of(c("manufacturer", "class"))) %>%
      mutate(
        panel = map_plot(data, ~
                           qplot(cty, hwy, data = .) + xlab("cty") + ylab("hwy") +
                           xlim(7, 37) + ylim(9, 47) + theme_bw())) %>%
      trelliscope(name = "tidy_gg", self_contained = TRUE, path = "www")
  })
})

shinyApp(ui = ui, server = server)

But I didn't manage to get a working example with facet_trelliscope.

stla commented 1 year ago

I think I get it now. One shouldn't use facet_trelliscope in Shiny, but trelliscope instead, in this way:

library(trelliscopejs)
library(dplyr)
library(ggplot2)
library(tidyr)

ui <- shinyUI(fluidPage(
  trelliscopeOutput(outputId = "plot")
))

server <- shinyServer(function(input, output) {
  output$plot <- renderTrelliscope({
    # nest gapminder data by country
    by_country <- gapminder %>%
      nest(data = !one_of(c("country", "continent")))
    # add in a plot column with map_plot
    by_country <- by_country %>% mutate(
      panel = map_plot(data, function(x) {
       ggplot(data = x) + geom_point(aes(x = year, y = lifeExp)) + 
          xlim(1948, 2011) + ylim(10, 95) + theme_bw() 
      }))
    # plot it
    by_country %>%
      trelliscope("gapminder", nrow = 2, ncol = 7, width = 300, 
                  self_contained = TRUE, path = "www")
  })

})

shinyApp(ui = ui, server = server)
hafen commented 1 year ago

Thanks we are working on better documentation for these kinds of things.