I would like to insert a function within the ggplot2 function for an interactive R Shiny visualization platform, and be able to keep the entire dataset and not have the data preprocessed.
For the sake of simplicity, let's assume that I want to take the mean of multiple groups.
In the pre-processed situation, I would take the mean of multiple groups, like so:
library(dplyr)
df <- data_long %>%
group_by(Variable, State, Scenario, Month) %>%
summarise_each(funs(mean))
Taking the preprocessed dataset, I run it through the R Shiny platform:
ui <- fluidPage(
titlePanel("Title"),
sidebarLayout(
sidebarPanel(
radioButtons("variableInput", "Variable",
choices = c("Income", "Electricity"),
selected = "Income"),
uiOutput("stateOutput")
),
mainPanel(
plotOutput("coolplot"),
br(), br(),
tableOutput("results")
)
)
)
server <- function(input, output, session) {
output$stateOutput <- renderUI({
selectInput("stateInput", "State",
sort(unique(df$State)),
selected = "New York")
})
filtered <- reactive({
if (is.null(input$stateInput)) {
return(NULL)
}
df %>%
filter(Variable == input$variableInput,
State == input$stateInput
)
})
output$coolplot <- renderPlot({
if (is.null(filtered())) {
return()
}
ggplot(filtered(), aes(x = Month, y = Value, group = Scenario, colour = Scenario)) +
geom_line() +
geom_point( size=4, shape=21, fill="white")
})
output$results <- renderTable({
filtered()
})
}
Instead, I would like to have the dplyr
grouping function be inserted into the ggplot portion in R Shiny and be able to keep the entire dataset. Any help would be appreciated. Questions and comments are welcome.
Edit
Instead of using the UI built for one plot, I'd also like to build a platform that supports multiple plots. For visualization purposes, it looks something like this:
ui <- fluidPage(
h1("Title"),
fluidRow(
column(width = 1.5, class = "panel",
selectInput("State", label = "State", width = "100%",
choices = c("New York", "New Jersey", "Texas", "California")),
selectInput("Variable", label = "Variable", width = "100%",
choices = c("Income", "Electricity"))
),
column(width = 11, "Main Plot",
highchartOutput("hcontainer", height = "500px"),
fixedRow(
column(3,
"Subplot 1",
highchartOutput("hcontainer",height = "500px")
),
column(3,
"Subplot 2",
highchartOutput("hcontainer",height = "500px")
),
column(3,
"Subplot 3",
highchartOutput("hcontainer",height = "500px")
)
)
)
)
)