Skip to content

Instantly share code, notes, and snippets.

@1danjordan
Last active January 14, 2019 12:51
Show Gist options
  • Save 1danjordan/538130bf3135bbe42efb7a2a1c33e323 to your computer and use it in GitHub Desktop.
Save 1danjordan/538130bf3135bbe42efb7a2a1c33e323 to your computer and use it in GitHub Desktop.
Data Quality Explorer Prototype
library(shiny)
library(plotly)
library(dplyr)
data(diamonds, package = "ggplot2")
df <- as_tibble(diamonds)
vars <- names(df)
numeric_vars <- df %>% select_if(is.numeric) %>% names()
categorical_vars <- df %>% select_if(~ !is.numeric(.x)) %>% names()
ui <- fluidPage(
headerPanel("Data Quality Explorer"),
# Inputs
sidebarPanel(
sliderInput('sample_size', 'Sample Size', min = 1, max = nrow(df), value = 30000, step = 500, round = 0),
selectInput('x', 'Variable', choices = numeric_vars, selected = "price"),
sliderInput('bins', 'Histogram Bins', min = 10, max = 200, value = 50),
sliderInput('plot_height', 'Height of plot (in pixels)', min = 100, max = 1500, value = 750),
selectInput('facet', 'Facet', categorical_vars, selected = "time_sk")
),
# UI Outputs
mainPanel(
tabsetPanel(
type = "tabs",
tabPanel("Variable Summary", uiOutput("dt")),
tabPanel("Histogram", plotlyOutput('histogram')),
tabPanel("Density Plot", plotlyOutput('density_plot'))
)
)
)
server <- function(input, output) {
dataset <- reactive({
df[sample(nrow(diamonds), input$sample_size),]
})
summaries <- df %>% mutate_if(is.integer, as.numeric) %>% skimr::skim_to_list()
summary_nms <- names(summaries)
observe({
lapply(summary_nms, function(summary) {
output[[paste0('summary_', summary)]] <- DT::renderDT(
summaries[[summary]],
style = "bootstrap",
options = list(pageLength = 20, lengthChange = FALSE))
})
})
output$dt <- renderUI({
tagList(lapply(summary_nms, function(summary) {
DT::dataTableOutput(paste0('summary_', summary))
}))
})
output$histogram <- renderPlotly({
p <- ggplot(dataset(), aes_string(input$x, fill = input$facet)) +
geom_histogram(bins = input$bins) +
theme_minimal() +
facet_wrap(input$facet, ncol = 1, scales = "free")
ggplotly(p, height = input$plot_height) %>%
layout(autosize=TRUE)
})
output$density_plot <- renderPlotly({
# build graph with ggplot syntax
p <- ggplot(dataset(), aes_string(input$x, fill = input$facet)) +
geom_density() +
theme_minimal() +
theme(axis.text.y = element_blank()) +
facet_wrap(input$facet, ncol = 1, scales = "free")
# if (input$facet_row) p <- p + facet_grid(input$facet_row)
ggplotly(p, height = input$plot_height) %>%
layout(autosize=TRUE)
})
}
shinyApp(ui, server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment