Reputation: 301
I have two data frames, "uno" and "dos" and I want to make a reactive bar plot considering the variables "tipo" and "fecha", i.e., I want to display the bar plot with data frame "uno" and group by "tipo" or "fecha", then do the same with data frame "dos". I can display a bar plot grouping only by one variable, but I don't know how to do it with more than one. I'm new in shiny so it's a little difficult for me to understand completlely the code's logic. I hope you can help me, thanks!
library(shiny)
library(tidyverse)
library(ggplot2)
uno <- data.frame(id = rnorm(10, 0, 1),
tipo = c("a", "a", "a", "b", "b", "b", "b", "c", "c", "a"),
fecha = c(12, 12, 12, 13, 13, 14, 15, 16, 16, 16))
dos <- data.frame(id = rnorm(10, 0, 1),
tipo = c("c", "a", "c", "c", "b", "b", "b", "c", "c", "a"),
fecha = c(11, 11, 12, 13, 13, 15, 15, 15, 16, 16))
datafiles <- list(uno, dos)
ui <- fluidPage(
selectInput('dataset', 'Choose Dataset', choices = c("uno" = "1", "dos" = "2")),
plotOutput('graph')
)
server = function(input, output, session){
outVar <- reactive({
temp <- datafiles[[as.numeric(input$dataset)]]
})
output$graph <- renderPlot({
ggplot(outVar(), aes(fecha)) + geom_bar()
})
}
shinyApp(ui=ui, server=server)
Upvotes: 1
Views: 718
Reputation: 125797
Maybe then this is what you are looking for. You can add a second selectInput to select the variable and map this input on x
in your call to ggplot
. As the input is a character you have to make use of e.g. the .data
pronoun:
library(shiny)
library(tidyverse)
library(ggplot2)
uno <- data.frame(id = rnorm(10, 0, 1),
tipo = c("a", "a", "a", "b", "b", "b", "b", "c", "c", "a"),
fecha = c(12, 12, 12, 13, 13, 14, 15, 16, 16, 16))
dos <- data.frame(id = rnorm(10, 0, 1),
tipo = c("c", "a", "c", "c", "b", "b", "b", "c", "c", "a"),
fecha = c(11, 11, 12, 13, 13, 15, 15, 15, 16, 16))
datafiles <- list(uno, dos)
ui <- fluidPage(
selectInput('dataset', 'Choose Dataset', choices = c("uno" = "1", "dos" = "2")),
selectInput('var', 'Choose Variable', choices = c("tipo", "fecha"), selected = "fecha"),
plotOutput('graph')
)
server = function(input, output, session){
outVar <- reactive({
temp <- datafiles[[as.numeric(input$dataset)]]
})
output$graph <- renderPlot({
ggplot(outVar(), aes(.data[[input$var]])) + geom_bar()
})
}
shinyApp(ui=ui, server=server)
Upvotes: 1