Open yanqingfu opened 3 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!
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)
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
.
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)
Thanks we are working on better documentation for these kinds of things.
following code no output