簡體   English   中英

反應棒 plot 與 Shiny 中的不同數據集

[英]Reactive bar plot with different datasets in Shiny

我有兩個數據框,“uno”和“dos”,我想制作一個反應條 plot 考慮變量“tipo”和“fecha”,即,我想顯示條形 plot 和數據框“uno”和組通過“tipo”或“fecha”,然后對數據框“dos”執行相同的操作。 我可以顯示一個條形 plot 僅按一個變量分組,但我不知道如何使用多個變量。 我是 shiny 的新手,所以我很難完全理解代碼的邏輯。 我希望你能幫助我,謝謝!

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)  

也許這就是你正在尋找的。 您可以在調用ggplot時在x上添加第二個 selectInput 到 select 變量和 map 這個輸入。 由於輸入是一個字符,您必須使用例如.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)  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM