2017-03-08 41 views
1

我有困难,了解如何访问由调用返回的封闭的元素reactive(...)闪亮R.访问一个无对象的元素闪亮

我有3个selectInput(...)和一些逻辑,允许在下拉列表中选择“全部” - 这是在dfFiltered顶部的_set()关闭中输入(但未显示)的内容。假设他们的工作只是与反应和renderPlot(...)通话罚款:他们让我来过滤日期,以及列内容,像这样:

dfFiltered <- eventReactive(input$summarize,{ 
    x <- x_set() 
    y <- y_set() 
    z <- z_set() 

    start_numeric <- as.numeric(as.Date(isolate(input$date_range[1]), format = "%Y%m%d")); 
    gt_start <- (data["updatedTimeYear"]*10000 + 
    data["updatedTimeMonth"]*100 + 
    data["updatedTimeDay"]) >= start_numeric; 
    end_numeric <- as.numeric(as.Date(isolate(input$date_range[2]), format = "%Y%m%d")); 
    lt_end <- (data["updatedTimeYear"]*10000 + 
    data["updatedTimeMonth"]*100 + 
    data["updatedTimeDay"]) <= end_numeric; 
    out <- dplyr::filter(data, 
    data$customer_env %in% envs & 
    data$customer_name %in% custs & 
    data$agent_name %in% sensors & 
    data$updatedDate >= start_numeric & 
    data$updatedDate <= end_numeric 
); 
    return(out); 
}) 

比方说,比如我有一个ggplot;再次我知道,因为我已经在目前的版本我闪亮的应用程序在使用这个就好了,我可以做这样的事情:

output$opens_plot <- renderPlot({ 
    ggplot(data = data.frame(dfFiltered())) + 
     facet_grid(. ~ factor(direction), scales = 'free') + 
     ggthemes::theme_fivethirtyeight(base_size = 14) + 
     stat_summary(aes(x = updatedTimeHour, y = n_opens, fill = factor(direction)), 
        color = 'black', 
        alpha = 0.6, 
        fun.y = function(x) sum(x), 
        geom = "bar") + 
     scale_y_continuous(labels=function(x) x/1000000) + 
     labs(
     x = 'Hour of Day', 
     y = 'Opens (Millions)', 
     title = 'Number of Opens, by Hour [Direction]') 
    ) 
    }) 

然而,当我转向更为复杂的绘图库,例如hichartsdygraphs我找到我自己需要直接访问封闭的列。 (这不是一个问题gg,因为我可以命名列。)我第一次尝试天真是这个样子:

hour <- dfFiltered()$updatedTimeHour 
    flows <- dfFiltered()$n_flows 

    output$flows_plot <- renderPlot({ 
    dygraph(day, flows, main = "N_Flows") %>% 
     dyRangeSelector(dateWindow = c('2/20/2017','3/10/2017')) 
     }) 

    # or... 

     output$flows_plot <- renderPlot({ 
     highchart() %>% 
     hc_title(text = "Scatter chart with size and color") %>% 
     hc_add_series(data = dfFiltered(), type = 'scatter', dfFiltered()$updatedTimeHour, dfFiltered()$updatedTimeHour, 
           dfFiltered()$direction) 
     }) 

我知道有可能是一个EAS(IER)的方式来概念化这个管道,从reactive - >list - >data.frame,但我不能换我围​​绕它的头......

+0

它应该像对待一个普通对象一样对待,只需缓存计算时间就可以更新。 ggplot()允许这种语法,无论对象是否被动 - 可能与其他绘图包相同。或者,也许我不了解这个问题。 –

回答

2

莱恩莫顿写道,只是把它当作一个普通的对象(在这种情况下,data.frame,被分配到局部变量df):

output$flows_plot <- renderPlot({ 
    df <- dfFiltered() 
    highchart() %>% 
    hc_title(text = "Scatter chart with size and color") %>% 
    hc_add_series(data = df, type = 'scatter', df$updatedTimeHour, df$updatedTimeHour, df$direction) 
    })