如何在R / Shiny中构建反应数据框? [英] How do I build a reactive dataframe in R / Shiny?

查看:175
本文介绍了如何在R / Shiny中构建反应数据框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用反应数据框显示多个绘图和图形。我有一个我想要过滤的数据集。我找到了正确的过滤器设置,我想显示一些不同的图表上的数据 - 如果过滤器设置被更改,将会更新。

I want to use a reactive dataframe to show multiple plots and graphs. I have a dataset that I would like to be able to filter on. Ones I have found the right filter settings, I would like to show the data on a number of different plots – that will update, if the filter settings are changed.

可能会解释,我正在做什么:

This might explain, what I’m trying to do:

UI:

fluidPage(
  sidebarLayout(
    sidebarPanel(

      checkboxGroupInput("checkGroups", 
                         label = "Include", choices = list("1 star" = 1, "2 star" = 2,
                                                           "3 star" = 3, "4 star" = 4,
                                                           "5 star" = 5),
                         selected = list(1, 2, 3, 4, 5)),

      checkboxInput("checkbox", "Include Replies"),

      actionButton("Button", "Update")

    ),
    mainPanel(

      showOutput("plot", "nvd3"),

      showOutput("pieplot", "nvd3")

    )
  )
)

服务器:

rating <- c(2, 3, 5, 4, 1, 5, 3, 1, 4)
date_time <- c("2015-05-14", "2015-05-07", "2015-05-06", "2015-04-11", "2015-01-07", "2014-12-06", "2014-04-11", "2014-01-07", "2013-12-06")
repliesOnly <- c(TRUE, FALSE, TRUE, TRUE, FALSE, TRUE, FALSE, FALSE, TRUE)
data <- data.frame(rating, date_time, repliesOnly)

function(input, output, session) {

  load("data.Rdata")

  newdata <- reactive({

    filter_data <- data

    filter_data <- filter_data %>% filter(rating %in% input$checkGroups)

    filter_data <- filter_data %>% filter(repliesOnly %in% input$checkbox)

    return(filter_data)

  })

  output$plot <- renderChart({

    plot <- nPlot(rating ~ date_time, data = newdata, 
                  type = "multiBarHorizontalChart", dom = 'plot')
    return(plot)

  })

  output$pieplot <- renderChart({

    pieplot <- nPlot(rating ~ date_time, data = newdata, 
                  type = "pieChart", dom = 'pieplot')
    return(pieplot)

  })

}

可以做吗?当然,我可以为每个图形输出包含过滤器,但是我的数据集是相当大的,我的过滤器是相当复杂的,所以如果它应该为每个图形计算它永远需要。

Can it be done? Of course I can just include the filter for each graph-output, but my dataset is rather big and my filter is quite complex, so if it should calculate it for each graph it takes forever.

所有的帮助深深感激!

推荐答案

感谢您的帮助 - 这工作:

Thanks for the help - this worked:

SERVER:

library(shiny)
library(rCharts)

rating <- c(2, 3, 5, 4, 1, 5, 3, 1, 4)
date_time <- c("2015-05-14", "2015-05-07", "2015-05-06", "2015-04-11", "2015-01-07", "2014-12-06", "2014-04-11", "2014-01-07", "2013-12-06")
repliesOnly <- c(TRUE, FALSE, TRUE, TRUE, FALSE, TRUE, FALSE, FALSE, TRUE)
data <- data.frame(rating, date_time, repliesOnly)
data$rating <- as.character(data$rating)


function(input, output, session) {

  load("data.Rdata")

  datadata <- data
  makeReactiveBinding("datadata")

  newData <- reactive({

    input$Button
    isolate({

      datadata <- data

      datadata <- subset(datadata, rating %in% input$checkGroups)


    })

  })

  output$plot <- renderChart({

    datadata <- newData()

    plot <- nPlot(rating ~ date_time, data = datadata, 
                  type = "multiBarHorizontalChart", dom = 'plot')
    return(plot)

  })

  output$pieplot <- renderChart({

    datadata <- newData()

    pieplot <- nPlot(rating ~ date_time, data = datadata, 
                  type = "pieChart", dom = 'pieplot')
    return(pieplot)

  })

}

这篇关于如何在R / Shiny中构建反应数据框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆