Skip to content

Instantly share code, notes, and snippets.

@hafen
Created November 6, 2019 03:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hafen/ef69d752b08cb1fe005461e55bde24b4 to your computer and use it in GitHub Desktop.
Save hafen/ef69d752b08cb1fe005461e55bde24b4 to your computer and use it in GitHub Desktop.
# need to install dev version of trelliscopejs
# remotes::install_github("hafen/trelliscopejs@dev")
library(shiny)
library(dplyr)
library(tidyr)
library(ggplot2)
library(trelliscopejs)
ui <- fixedPage(
title = "Test Shiny App with TrelliscopeJS",
fixedRow(
column(width = 4,
tags$br(),
selectInput("example",
label = "Choose Example", choices = c("example 1", "example 2")),
actionButton("make_trelliscope", "Create Trelliscope Display")
),
column(
width = 8,
# dimensions for trelliscopeOutput should be in units of "px"
trelliscopeOutput("trelliscope_out",
width = "800px", height = "800px")
)
)
)
server <- function(input, output, session) {
output$trelliscope_out <- renderTrelliscope({
if (input$make_trelliscope == 0) {
NULL
} else {
ex <- isolate(input$example)
if (ex == "example 1") {
x <- factor(LETTERS[1:4])
names(x) <- letters[1:4]
data_year_date <- data.frame(
year = structure(seq(1950, 1989, 1), class = "Date"),
var = rep(x, 10), y = seq(1, 200, 5))
# trelliscope displays must go in www/...
# also, can do self_contained = TRUE, but this is not advised
p <- ggplot(data_year_date, aes(year, y)) + geom_point() +
facet_trelliscope(~ var, path = "www/tr_example1")
# if output is from using facet_trelliscope, need to print it
print(p)
} else if (ex == "example 2") {
mpg %>%
group_by(manufacturer, class) %>%
nest() %>%
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", path = "www/tr_example2",
nrow = 2, ncol = 2)
} else {
NULL
}
}
})
}
shinyApp(ui = ui, server = server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment