2015-02-09 32 views
1

我有一个使用ggvis()图形的基本Shiny应用程序。该应用程序在下面给出。使用ggvis中的handle_click在Shiny中创建一个交互式过滤器

我在ggvis()的文档中看到有一个handle_click(vis, on_click = NULL)函数可以通过。此外,on_click是一个带参数数据,位置和会话的回调函数。

我想要做的是允许用户点击图表中的一个条(每个月一个条),并设置input$monthSelect到他们点击的条的月份。如果我添加了一个工具提示,我可以创建一个函数,从该图层中的数据中提取数据,我可以参考data$x_获取该月份。

这个功能的一个例子是:

update_selection = function(data){ 
    if(is.null(data)) return(NULL) 
    updateSelectInput(session 
        ,"monthSelect" 
        ,selected=data$x_) 
    } 

,我把它添加到ggvis通过ggvis() %>% handle_click(update_selection(data)),但我得到一个错误Error in func() : could not find function "fun"

我该如何制作一个反应图?

library(ggvis) 
library(dplyr) 
library(tidyr) 
library(ReporteRs) 
data = cocaine 
data = within(data, 
    { 
    month[month==1] = "January" 
    month[month==2] = "February" 
    month[month==3] = "March" 
    month[month==4] = "April" 
    month[month==5] = "May" 
    month[month==6] = "June" 
    month[month==7] = "July" 
    month[month==8] = "August" 
    month[month==9] = "September" 
    month[month==10] = "October" 
    month[month==11] = "November" 
    month[month==12] = "December" 
    } 
) 

server = function(input, output, session){ 

    selectedState = reactive(input$stateSelect) 

    plotData = reactive({ 
    data %>% 
     group_by(state,month) %>% 
     summarise(avgPotency = mean(potency)) %>% 
     ungroup() %>% 
     spread(month,avgPotency) %>% 
     mutate(January = ifelse(is.na(January),0,January) 
      ,February = ifelse(is.na(February),0,February) 
      ,March = ifelse(is.na(March),0,March) 
      ,April = ifelse(is.na(April),0,April) 
      ,May = ifelse(is.na(May),0,May) 
      ,June = ifelse(is.na(June),0,June) 
      ,July = ifelse(is.na(July),0,July) 
      ,August = ifelse(is.na(August),0,August) 
      ,September = ifelse(is.na(September),0,September) 
      ,October = ifelse(is.na(October),0,October) 
      ,November = ifelse(is.na(November),0,November) 
      ,December = ifelse(is.na(December),0,December) 
    ) %>% 
     filter(state==selectedState()) %>% 
     gather("month","AvgPotency",-state) 
    }) 

    stateVis = reactive({ 
    plotData() %>% 
     ggvis(x=~month,y=~AvgPotency) 
    }) 
    stateVis %>% bind_shiny("cocaineCounts") 

    selectedMonth = reactive(input$monthSelect) 
    tableData = reactive({ 
    data %>% 
     filter(state==selectedState() & month==selectedMonth()) 
    }) 
    output$cocaineTable = renderUI({ 
    MyFTable = FlexTable(tableData(), 
         header.cell.props = cellProperties(padding = 2), 
         body.cell.props = cellProperties(padding = 2)) 
    return(HTML(as.html(MyFTable))) 
    }) 

} 

ui = shinyUI(
    fluidPage(
    column(6, 
      selectInput("stateSelect",label="Select a State:",choices = unique(data$state),selected=1) 
      ,selectInput("monthSelect",label="Select a Month:",choices = c('January','February','March','April','May','June','July','August','September','October','November','December'),selected=1) 
      ,ggvisOutput("cocaineCounts") 
    ) 
    ,column(6, 
      uiOutput(outputId = "cocaineTable") 
    ) 
    ) 
) 

shinyApp(ui = ui, server = server) 

回答

4

功能需要更新,接受三个参数:需要

update_selection = function(data,location,session){ 
    if(is.null(data)) return(NULL) 
    updateSelectInput(session 
        ,"monthSelect" 
        ,selected=data$x_) 
    } 

和handle_click()作为ggvis() %>% handle_click(update_selection)

要传递这样的完整,工作程序是:

library(ggvis) 
library(dplyr) 
library(tidyr) 
library(ReporteRs) 
data = cocaine 
data = within(data, 
    { 
    month[month==1] = "January" 
    month[month==2] = "February" 
    month[month==3] = "March" 
    month[month==4] = "April" 
    month[month==5] = "May" 
    month[month==6] = "June" 
    month[month==7] = "July" 
    month[month==8] = "August" 
    month[month==9] = "September" 
    month[month==10] = "October" 
    month[month==11] = "November" 
    month[month==12] = "December" 
    } 
) 

update_selection = function(data,location,session){ 
    if(is.null(data)) return(NULL) 
    updateSelectInput(session 
        ,"monthSelect" 
        ,selected=data$x_) 
    } 

server = function(input, output, session){ 

    selectedState = reactive(input$stateSelect) 

    plotData = reactive({ 
    data %>% 
     group_by(state,month) %>% 
     summarise(avgPotency = mean(potency)) %>% 
     ungroup() %>% 
     spread(month,avgPotency) %>% 
     mutate(January = ifelse(is.na(January),0,January) 
      ,February = ifelse(is.na(February),0,February) 
      ,March = ifelse(is.na(March),0,March) 
      ,April = ifelse(is.na(April),0,April) 
      ,May = ifelse(is.na(May),0,May) 
      ,June = ifelse(is.na(June),0,June) 
      ,July = ifelse(is.na(July),0,July) 
      ,August = ifelse(is.na(August),0,August) 
      ,September = ifelse(is.na(September),0,September) 
      ,October = ifelse(is.na(October),0,October) 
      ,November = ifelse(is.na(November),0,November) 
      ,December = ifelse(is.na(December),0,December) 
    ) %>% 
     filter(state==selectedState()) %>% 
     gather("month","AvgPotency",-state) 
    }) 

    stateVis = reactive({ 
    plotData() %>% 
     ggvis(x=~month,y=~AvgPotency) %>% 
     handle_click(update_selection) 
    }) 
    stateVis %>% bind_shiny("cocaineCounts") 

    selectedMonth = reactive(input$monthSelect) 
    tableData = reactive({ 
    data %>% 
     filter(state==selectedState() & month==selectedMonth()) 
    }) 
    output$cocaineTable = renderUI({ 
    MyFTable = FlexTable(tableData(), 
         header.cell.props = cellProperties(padding = 2), 
         body.cell.props = cellProperties(padding = 2)) 
    return(HTML(as.html(MyFTable))) 
    }) 

} 

ui = shinyUI(
    fluidPage(
    column(6, 
      selectInput("stateSelect",label="Select a State:",choices = unique(data$state),selected=1) 
      ,selectInput("monthSelect",label="Select a Month:",choices = c('January','February','March','April','May','June','July','August','September','October','November','December'),selected=1) 
      ,ggvisOutput("cocaineCounts") 
    ) 
    ,column(6, 
      uiOutput(outputId = "cocaineTable") 
    ) 
    ) 
) 

shinyApp(ui = ui, server = server)