Shiny 中具有不同数据集的反应式条形图
Reactive bar plot with different datasets in Shiny
我有两个数据框,“uno”和“dos”,我想根据变量“tipo”和“fecha”制作一个反应式条形图,即,我想显示带有数据框的条形图“uno”并按“tipo”或“fecha”分组,然后对数据框“dos”执行相同操作。我可以显示仅按一个变量分组的条形图,但我不知道如何处理多个变量。我是闪亮的新手,所以完全理解代码的逻辑对我来说有点困难。希望大家能帮帮我,谢谢!
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)
也许这就是您要找的。您可以将第二个 select 输入添加到 select 变量,并在您对 ggplot
的调用中将此输入映射到 x
。由于输入是一个字符,您必须使用例如.data
代词:
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)
我有两个数据框,“uno”和“dos”,我想根据变量“tipo”和“fecha”制作一个反应式条形图,即,我想显示带有数据框的条形图“uno”并按“tipo”或“fecha”分组,然后对数据框“dos”执行相同操作。我可以显示仅按一个变量分组的条形图,但我不知道如何处理多个变量。我是闪亮的新手,所以完全理解代码的逻辑对我来说有点困难。希望大家能帮帮我,谢谢!
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)
也许这就是您要找的。您可以将第二个 select 输入添加到 select 变量,并在您对 ggplot
的调用中将此输入映射到 x
。由于输入是一个字符,您必须使用例如.data
代词:
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)