This is a Shiny web application for data visualization. Click for app view: https://kenpyfin.shinyapps.io/ShinyHW/
In [ ]:
#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
library(shiny)
library(DT)
server <- function(input,output, session) {
library(ggplot2)
library(shiny)
library(DT)
# library(stringr)
#setwd("C:/Users/kli4/Downloads/Shiny_HW")
dataset = read.csv("dataset.csv")
dataset$AGE_Bin = cut(dataset$AGE,5,include.lowest = TRUE)
dataset$INCOME_Bin = cut(dataset$INCOME,5,include.lowest = TRUE,dig.lab = 6)
# dataset$INCOME_Bin <- lapply(strsplit(gsub("]|[[(]", "", levels(dataset$INCOME_Bin)), ","),
# prettyNum, big.mark=".", decimal.mark=",", input.d.mark=".", preserve.width="individual")
plot_var <- eventReactive(input$gobutton,{
selection <- input$option
data_agg <-aggregate(x=dataset$Customer, by=list(SELECTION=dataset[,c(selection)],TREATMENT = dataset[,"TREATMENT"]),length)
names(data_agg) = c("SELECTION","TREATMENT", "Customer")
return(data_agg)
})
output$disPlot <- renderPlot({
displot = ggplot(plot_var(), aes(x=SELECTION,y=Customer,fill=TREATMENT)) + geom_bar(position="stack",stat="identity")
output$disPlot_download <- downloadHandler(
filename = function() { paste(input$option, '.jpg', sep='') },
content = function(file){
ggsave(file,plot=displot)
})
displot
})
output$table <- DT::renderDataTable(datatable(dataset))
scatter_plot <- ggplot(dataset, aes(x=AGE,y=INCOME)) + geom_point()
scatter_plot = scatter_plot + facet_grid(GENDER ~ TREATMENT)
output$Scatter <- renderPlot({
scatter_plot
})
scatter_brushed <- reactive({
my_brush <- input$selected_range
sel_range <- brushedPoints(dataset, my_brush)
return(sel_range)
})
output$brushed_table <- DT::renderDataTable(DT::datatable(scatter_brushed()))
displot2 <- ggplot(dataset, aes(online.Activity.A)) + geom_histogram(aes(fill=AGE_Bin), bins = 5)
displot2 = displot2 + facet_grid(GENDER ~ TREATMENT)
displot3 <- ggplot(dataset, aes(online.ACTIVITY.B)) + geom_histogram(aes(fill=AGE_Bin), bins = 5)
displot3 = displot3 + facet_grid(GENDER ~ TREATMENT)
output$displot2 <- renderPlot({
displot2
})
output$displot3 <- renderPlot({
displot3
})
#
# scatter_brushed2 <- reactive({
#
# my_brush <- input$selected_range2
# sel_range <- brushedPoints(dataset, my_brush)
# return(sel_range)
#
# })
# output$brushed_table2 <- DT::renderDataTable(DT::datatable(scatter_brushed2()))
data_agg2 <-aggregate(list(Activity_A=dataset$online.Activity.A), by=list(DAY=dataset$DAY,TREATMENT=dataset$TREATMENT,GENDER=dataset$GENDER),mean)
lineplot <- ggplot(data_agg2, aes(x=DAY, y=Activity_A, group=c(TREATMENT))) + geom_line(aes(color=TREATMENT)) + geom_point()
lineplot = lineplot + facet_grid(GENDER ~ TREATMENT)
output$lineplot <- renderPlot({
lineplot
})
data_agg2 <-aggregate(list(Activity_B=dataset$online.ACTIVITY.B), by=list(DAY=dataset$DAY,TREATMENT=dataset$TREATMENT, GENDER=dataset$GENDER),mean)
lineplot2 <- ggplot(data_agg2, aes(x=DAY, y=Activity_B, group=c(TREATMENT))) + geom_line(aes(color=TREATMENT)) + geom_point()
lineplot2 = lineplot2 + facet_grid(GENDER ~ TREATMENT)
output$lineplot2 <- renderPlot({
lineplot2
})
#Downloads
output$lineplot2_download <- downloadHandler(
filename = "Activity_B Line.jpg",
content = function(file){
ggsave(file,plot=lineplot2)
})
output$lineplot_download <- downloadHandler(
filename = "Activity_A Line.jpg",
content = function(file){
ggsave(file,plot=lineplot)
})
output$displot2_download <- downloadHandler(
filename = "ActivityA_Dist.jpg",
content = function(file){
ggsave(file,plot=displot2)
})
output$displot3_download <- downloadHandler(
filename = "ActivityB_Dist.jpg",
content = function(file){
ggsave(file,plot=displot3)
})
output$scatter_download <- downloadHandler(
filename = "Age_Income.jpg",
content = function(file){
ggsave(file,plot=scatter_plot)
})
}
ui <- fluidPage(
h1("Rshiny Homework"),
h2("Demographic Exploartion"),
h3("Filterable Table"),
DT::dataTableOutput("table"),
br(),
h3("Charts"),
selectInput(
"option",
"Demography",
c("AGE_Bin","INCOME_Bin","GENDER"),
selected = NULL,
multiple = FALSE,
selectize = TRUE,
width = NULL,
size = NULL
),
actionButton("gobutton", "View Chart", class = "btn-success"),
plotOutput("disPlot"),
downloadButton(outputId = "disPlot_download", label = "Download Chart",class = "btn-success"),
br(),
hr(),
br(),
h3("Relationship Between Variables"),
tabsetPanel(
tabPanel("Scatter",
plotOutput("Scatter", brush="selected_range"),
br(),
downloadButton(outputId = "scatter_download", label = "Download Chart",class = "btn-success"),
br(),
br(),
DT::dataTableOutput("brushed_table")
),
tabPanel("Distribution",
plotOutput("displot2"),
downloadButton(outputId = "displot2_download", label = "Download Chart",class = "btn-success"),
br(),
plotOutput("displot3"),
downloadButton(outputId = "displot3_download", label = "Download Chart",class = "btn-success")
)
),
br(),
hr(),
br(),
h3("Line Plot"),
plotOutput("lineplot"),
downloadButton(outputId = "lineplot_download", label = "Download Chart",class = "btn-success"),
br(),
plotOutput("lineplot2"),
downloadButton(outputId = "lineplot2_download", label = "Download Chart",class = "btn-success")
)
shinyApp(ui = ui, server = server)