【问题标题】:How do you filter a data frame in a shiny document and display a datatable?如何过滤闪亮文档中的数据框并显示数据表?
【发布时间】:2019-12-13 19:32:13
【问题描述】:

我正在尝试过滤数据框,然后对数据进行一些简单的 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))
})
```

谁能指出我哪里出错了?当我运行代码时,我得到了我的下拉框,但仅此而已。没有错误。只是没有过滤的数据表。

谢谢。

【问题讨论】:

    标签: shiny shiny-reactivity


    【解决方案1】:

    您的反应式的右括号在错误的位置。过滤数据后,它们应该关闭。

    ---
    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))
    
    ```
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-04
      • 2017-05-05
      • 1970-01-01
      • 2021-12-19
      • 2015-07-14
      • 2018-07-14
      • 2014-05-13
      相关资源
      最近更新 更多