r-Shiny中不同数据集的反应条形图



我有两个数据帧,"uno";以及";dos";并且我想在考虑变量";tipo";以及";fecha";,即我想显示具有数据帧的条形图";uno";并按";tipo";或";fecha";,则对数据帧"1"进行同样的处理;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)  

也许这就是您想要的。您可以添加第二个selectInput来选择变量,并将调用中x上的输入映射到ggplot。由于输入是一个字符,您必须使用例如.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)  

最新更新