在R Shiny

时间:2017-06-15 15:47:26

标签: r ggplot2 shiny dplyr

我想在ggplot2函数中为交互式R Shiny可视化平台插入一个函数,并且能够保留整个数据集而不对数据进行预处理。

为简单起见,我们假设我想采用多个群体的平均值。

在预处理的情况下,我会采用多个组的平均值,如下:

library(dplyr)
df <- data_long %>%
  group_by(Variable, State, Scenario, Month) %>%
  summarise_each(funs(mean))

使用预处理的数据集,我通过R Shiny平台运行它:

ui <- fluidPage(
  titlePanel("Title"),
  sidebarLayout(
    sidebarPanel(
      radioButtons("variableInput", "Variable",
                   choices = c("Income", "Electricity"),
                   selected = "Income"),
      uiOutput("stateOutput")
    ),
    mainPanel(
      plotOutput("coolplot"),
      br(), br(),
      tableOutput("results")
    )
  )
)

server <- function(input, output, session) {
  output$stateOutput <- renderUI({
    selectInput("stateInput", "State",
                sort(unique(df$State)),
                selected = "New York")
  })  

  filtered <- reactive({
    if (is.null(input$stateInput)) {
      return(NULL)
    }    

    df %>%
      filter(Variable == input$variableInput,
             State == input$stateInput
      )
  })

  output$coolplot <- renderPlot({
    if (is.null(filtered())) {
      return()
    }
    ggplot(filtered(), aes(x = Month, y = Value, group = Scenario, colour = Scenario)) +
      geom_line() +
      geom_point( size=4, shape=21, fill="white")
  })

  output$results <- renderTable({
    filtered()
  })
}

相反,我希望将dplyr分组函数插入到R Shiny的ggplot部分中,并能够保留整个数据集。任何帮助,将不胜感激。欢迎提出问题和意见。

修改

我还希望构建一个支持多个绘图的平台,而不是使用为一个绘图构建的UI。出于可视化目的,它看起来像这样:

ui <- fluidPage(
  h1("Title"),
  fluidRow(
    column(width = 1.5, class = "panel",
           selectInput("State", label = "State", width = "100%",
                       choices = c("New York", "New Jersey", "Texas", "California")), 
           selectInput("Variable", label = "Variable",  width = "100%",
                       choices = c("Income", "Electricity"))
    ),
    column(width = 11, "Main Plot",
           highchartOutput("hcontainer", height = "500px"),
           fixedRow(
             column(3,
                    "Subplot 1",
                    highchartOutput("hcontainer",height = "500px")
                  ),
             column(3,
                    "Subplot 2",
                    highchartOutput("hcontainer",height = "500px")
                    ),
             column(3,
                    "Subplot 3",
                    highchartOutput("hcontainer",height = "500px")
                    )
           )
    )
  )
)

0 个答案:

没有答案