将无功功能输出传递给另一个无功功能(闪亮)

时间:2016-02-19 01:27:26

标签: r shiny dplyr

我有一个看起来如下的数据集(但实际的数据集要大得多,并且包含一些额外的列):

"month","year","id","hour","count"
1,2015,"place001",13,10.2319100637371
6,2015,"place001",14,42.2066290129154
10,2015,"place002",18,34.5326964651126
3,2015,"place002",8,23.0217976434084
3,2014,"place002",1,19.184831369507
3,2014,"place003",15,47.322584044784

我正在尝试构建一个Shiny应用来探索这些数据。这是我到目前为止所得到的,但它在尝试处理quarterly反应函数时会中断。如果我使用已注释掉的“冗余”方法,应用程序会正确呈现,但我想避免使用它。

稍后我还需要使用totals的输出并使用melt函数对其进行重新整形,因此我想了解如何将此反应函数用作另一个反应函数的输入。< / p>

非常感谢任何帮助。

ui.R

library(shiny)
library(ggvis)

renderInputs <- function() {
  wellPanel(
    ## Take hour information from user for filtering.
    checkboxGroupInput("hour", "Hours",
                       c("1" = 1,
                         "8" = 8,
                         "13" = 13,
                         "14" = 14,
                         "15" = 15,
                         "18" = 18),
                       selected  = c(1, 8, 13, 15),
                       inline = T)
  )
}

shinyUI(fluidPage(
  titlePanel("Analysis"),

  fluidRow(
    ## Inputs
    column(4, renderInputs()),
    ## Plot data.
    column(8,h2("Monthly Visitors"),
           ggvisOutput("plotM")
    )),
    ## Plot quarterly data.
  fluidRow(
    column(7, h2('Quarterly Summary'),
    ggvisOutput("plotQ"))
    )
))

server.R

library(shiny)
library(dplyr)
library(zoo)

data <- read.csv("file.csv")
data$date <- as.yearmon(paste0(data$year, "-", data$month))

shinyServer(function(input, output) {

    totals <- reactive({
      data %>%
        filter(hour %in% input$hour) %>%
        group_by(date) %>%
        summarize(visitors = sum(count))
    })

    ## Quarterly, with "reactive" approach.
    quarterly <- reactive({
      totals() %>%
        mutate(quarter = as.yearqtr(date)) %>%
        group_by(quarter) %>%
        summarize(visitors = sum(count))
    })


    ## Quarterly data and percentage changes (redudant approach).
#    quarterly <- reactive({
#    data %>%
#      filter(hour %in% input$hour) %>%
#      mutate(quarter = as.yearqtr(date)) %>%
#      group_by(quarter) %>%
#      summarize(visitors = sum(count))
#    })

    ## Create estimated quarterly chart.
    visM <- reactive({
      totals() %>%
        ggvis(~as.factor(date), ~visitors) %>%
        layer_points()
    })  

    ## Create estimated quarterly chart.
    visQ <- reactive({
      quarterly() %>%
        ggvis(~as.factor(quarter), ~visitors) %>%
        layer_bars(fill := "green")
    })


    ## Send plots to UI.
    visM %>% bind_shiny("plotM")

    visQ %>% bind_shiny("plotQ")
})

感谢。

1 个答案:

答案 0 :(得分:0)

谢谢@NicE,我完全错过了!在解决了这个愚蠢的错误后,它正常工作。感谢K. Rohde。

## Quarterly, with "reactive" approach.
quarterly <- reactive({
  totals() %>%
    mutate(quarter = as.yearqtr(date)) %>%
    group_by(quarter) %>%
    summarize(visitors = sum(visitors))
})