I am creating a shiny application consisting in plotting data from different datasets.
Basically user has to choose two times using one radiobutton and one selectinput for getting desired plot. Here is a minimal example:
#
# 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/
#
# create data
name <- c("Jon", "Bill", "Maria")
age <- c(23, 41, 32)
d1 <- data.frame(name, age)
employee <- c('John Doe','Peter Gynn','Jolie Hope')
salary <- c(21000, 23400, 26800)
startdate <- as.Date(c('2010-11-1','2008-3-25','2007-3-14'))
d2 <- data.frame(employee, salary, startdate)
library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
radioButtons("ind1", "Choose indicator:",
c("Q1" = "q1",
"Q2" = "q2")
)
),
# Show a plot of the generated distribution
mainPanel(
selectInput("ind2", "Choose metrics:",
c("M1" = "m1",
"M2" = "m2"),
plotOutput("gmplot"))
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
# Create a "data_source" reactive variable
data_intacc <- reactive({
# Return the appropriate data source depending on
# the chosen radio button
if (input$ind1 == "q1" & input$ind2 == "m1") {
data <- d1
} else if (input$ind1 == "q2" & input$ind2 == "m2") {
data <- d2
}
return(data)
})
output$gmplot <-renderPlot({
data = data_intacc()
p1 <- ggplot(data, aes(x, y, fill)) +geom_bar(stat= "identity")
print(p1)
})
}
# Run the application
shinyApp(ui = ui, server = server)
My problem is that based on user input data is different, so I need to figure out one way to subset reactive data inside ggplot2 to define aes(x, y) because x and y will be different depending on user input.
Any idea how to deal with this case?
Thank you