如何过滤发光文档中的数据框并显示数据表?

时间:2019-12-13 19:32:13

标签: shiny shiny-reactivity

我正在尝试过滤数据帧,然后对数据进行一些简单的ggplots。我已经尝试利用Shiny文档上的R Studio示例以及有关该主题的以下SO帖子:

Reactively filtering/subsetting a data frame in shiny

这是我的代码。

---
title: "Shiny Filter Test"
author: "Novice"
date: "12/13/2019"
output: html_document
runtime: shiny
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

```


```{r}

library(tidyverse)
library(shiny)
inputPanel(
  selectInput("n_break", label = "Number of bins:",
              choices = c(10, 20, 35, 50), selected = 10)
)

cdat <- reactive({

data <- tibble(x = c(10,20,35), y = c("a","b","c"))

  data %>% 
  filter(x %in% input$n_break)

output$table <- DT::renderDT({
        cdat()
    }, options = list(scrollX = TRUE))
})
```

有人可以指出我要去哪里了吗?运行代码时,我会看到下拉框,仅此而已。没有错误。只是没有过滤的数据表。

谢谢。

1 个答案:

答案 0 :(得分:0)

反应堆的右括号放在错误的位置。一旦您过滤了数据,它们就会关闭。

---
title: "Shiny Filter Test"
author: "Novice"
date: "12/13/2019"
output: html_document
runtime: shiny
---

```{r setup}
knitr::opts_chunk$set(
    echo = FALSE
)
```

```{r}
library(tidyverse)
library(shiny)

inputPanel(
  selectInput("n_break", label = "Number of bins:",
              choices = c(10, 20, 35, 50), selected = 10)
)

cdat <- reactive({
    data <- tibble(x = c(10,20,35), y = c("a","b","c"))  
    data %>% filter(x %in% input$n_break)
})

DT::renderDT({
  cdat()
}, options = list(scrollX = is ))

```

关于reactive的说明:如果您打算进一步扩展此过滤器数据,以便在其他地方使用过滤后的数据,则可以在reactive函数中进行过滤。但是,如果不是这种情况,我将在renderDT内进行过滤:

---
title: "Shiny Filter Test"
author: "Novice"
date: "12/13/2019"
output: html_document
runtime: shiny
---

```{r setup}
knitr::opts_chunk$set(
    echo = FALSE
)
```


```{r}
library(tidyverse)
library(shiny)

data <- tibble(x = c(10,20,35), y = c("a","b","c"))

inputPanel(
  selectInput("n_break", label = "Number of bins:",
              choices = c(10, 20, 35, 50), selected = 10)
)

DT::renderDT({
  data %>% filter(x %in% input$n_break)
}, options = list(scrollX = TRUE))

```