Skip to content

Instantly share code, notes, and snippets.

@corynissen
Created August 27, 2014 15:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save corynissen/25d5144bbe055381a188 to your computer and use it in GitHub Desktop.
Save corynissen/25d5144bbe055381a188 to your computer and use it in GitHub Desktop.
ggvis hack...
library(shiny)
library(ggvis)
shinyServer(function(input, output, session) {
df <- mtcars
df$id <- 1:nrow(df)
plot_data <- reactive({
df$x <- df[,input$x_var]
# do some dynamic change of the data...
#df <- df[-c(1:runif(1, 1, 10)),]
df$include <- rep(TRUE, nrow(df))
df$include[c(1:runif(1, 1, 10))] <- FALSE
df$size <- rep(50, nrow(df))
df$fill <- lb$fill()
df$size[!df$include] <- .0001
df$color[!df$include] <- "white"
df
})
selected_plot_data <- reactive({
df <- plot_data()
df$selected <- lb$selected()
df
})
lb <- linked_brush(keys = df$id, "red")
plot_data %>%
ggvis(~x, ~mpg) %>%
layer_points(fill := ~fill, size := ~size, fill.brush := "red") %>%
lb$input() %>%
set_options(width=800) %>%
bind_shiny('gg', 'gg_ui')
output$table <- renderDataTable({
df <- selected_plot_data()
if(any(df$selected)){df <- subset(df, selected)}
df <- subset(df, include)
df
}, options=list(bPaginate=FALSE))
})
library(shiny)
library(ggvis)
shinyUI(pageWithSidebar(
headerPanel('mtcars'),
sidebarPanel(
tags$head(
tags$style(type="text/css", "label.radio { display: inline-block; }", ".radio input[type=\"radio\"] { float: none; }"),
tags$style(type="text/css", "select { max-width: 200px; }"),
tags$style(type="text/css", "textarea { max-width: 185px; }"),
tags$style(type="text/css", ".jslider { max-width: 200px; }"),
tags$style(type='text/css', ".well { max-width: 250px; }"),
tags$style(type='text/css', ".span4 { max-width: 250px; }")
),
selectInput(inputId = "x_var",
label = "Select X Variable to Display",
choices = c("drat",
"wt",
"hp"),
selected = "wt")
),
mainPanel(
uiOutput('gg_ui'),
ggvisOutput('gg'),
dataTableOutput("table")
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment