如何在R / Shiny中构建响应式数据框?

14

我希望使用响应式数据框来展示多个图形和图表。我有一个数据集,希望能够进行筛选。一旦找到了正确的筛选设置,我想在多个不同的绘图上显示数据 - 如果更改了筛选设置,这些图将会更新。

这或许能够解释我正在尝试做什么:

用户界面:

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)

  })

}

能否实现?当然我可以为每个图形输出包含过滤器,但我的数据集相当大且我的过滤器相当复杂,因此如果它应该为每个图形计算它,那将需要很长时间。

非常感谢所有的帮助!


你从哪里获得了showOutput函数?请确保在发布示例代码之前它是可复制的。 - nrussell
3
似乎你所要做的就是在输出渲染命令中添加类似 dat <- newdata() 的内容,并在绘图命令中使用 dat - Rorschach
4
实际上,我认为你只需在newdata后面加上 (),就像你写的那样,然后调用newdata即可。 - Rorschach
2个回答

11
感谢您的帮助-这个方法有效:

服务器:

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)

  })

}

6
请考虑添加评论。 - Hack-R

8
感谢您提供这个示例;我还需要一种响应式的数据框过滤器。在与以下错误抗争了2个小时后:
cannot coerce class ""reactive"" to a data.frame

你的帖子帮助了我解决了问题。我敢于发布一个简化版本的例子,更容易复制(没有showOutputreadChart调用),供其他shiny用户参考。

注:我使用单文件应用程序。

library(shiny)

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)

ui <- fluidPage(
   sidebarLayout(
   sidebarPanel(

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

mainPanel(
  tableOutput("view")
    )
   )
    )

server <- function(input, output, session) {
  newData <- reactive({
         data <- subset(data, rating %in% input$checkGroups)
 })

output$view <- renderTable({ newData() })

}

shinyApp(ui = ui, server = server)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接