在 Shiny 应用程序中过滤数据框 [英] Filter data frame in Shiny app

查看:41
本文介绍了在 Shiny 应用程序中过滤数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用用户输入作为单选按钮过滤数据框.不幸的是,只有一种类型的过滤器有效(在我的示例中为年度"版本),但每月"和季度"选项没有返回任何内容.这是我的示例数据集和代码.

I'm trying to filter a data frame with user input as radio buttons. Unfortunately, only one type of filter works (the "Annual" version in my example), but the "Monthly" and "Quarterly" options are not returning anything. Here is my sample data set and code.

    # sample data
mydf <- data.frame("Data"=rnorm(12), 
                   "Months"=c("Jan", "Nov", "Dec", "Feb", 
                              "Mar", "Apr", "May", "Jun", 
                              "Jul", "Aug", "Sep", "Oct"))
library(shiny)
library(dbplyr)
ui <- fluidPage(
        # Input() function
        radioButtons(inputId = "myDateInterval", label = "Select Date Interval",
                     choiceNames = list("Monthly","Quarterly","Annual"),
                     choiceValues = list(unique(as.character(mydf$Month)),
                                         unique(as.character(mydf$Month))
                                      [seq(1,length(unique(mydf$Month)),3)],
                                         unique(as.character(mydf$Month)[1]))),

        # Output() functions
        tableOutput("results"))
# set up server object
server <- function(input, output) {
        output$results <-  renderTable({
                mydf %>% filter(Months %in% input$myDateInterval)
        })
}
shinyApp(ui = ui, server = server)

推荐答案

文档对这个限制不是很清楚,但是在

The documentation is not very clear about this limitation, but in

https://blog.rstudio.com/2017/04/05/shiny-1-0-1/

你发现

choiceValues 中的元素必须仍然是纯文本(这些是用于计算的值).但是 choiceNames 中的元素(UI标签)可以用 HTML 构建,或者使用 HTML()函数,或 HTML 标签生成函数,如 tags$img() 和图标().

The elements in choiceValues must still be plain text (these are the values used for computation). But the elements in choiceNames (the UI labels) can be constructed out of HTML, either using the HTML() function, or an HTML tag generation function, like tags$img() and icon().

纯文本是必需的,因为它必须跨越 JS 和 R 之间的边界.您可以使用 JSON 作为传输器;我真的不推荐这里,但它很容易:

Plain text is required because it has to cross the border between JS and R. You could use JSON as a transporter; I do not really recommend it here, but it is fairly easy:

library(jsonlite)
library(shiny)
mydf <- data.frame("Data"=rnorm(12), 
                   "Months"=c("Jan", "Nov", "Dec", "Feb", 
                   "Mar", "Apr", "May", "Jun", 
                   "Jul", "Aug", "Sep", "Oct"), stringsAsFactors = FALSE)
ui <- fluidPage(
  # Input() function
  radioButtons(inputId = "myDateInterval", label = "Select Date Interval",
               choiceNames = list("Monthly","Quarterly","Annual"),
               choiceValues = list(toJSON(mydf$Month),
                                   toJSON(mydf$Month[seq(1,length(unique(mydf$Month)),3)]),
                                   toJSON(mydf$Month[1]))),

  # Output() functions
  tableOutput("results"))
# set up server object
server <- function(input, output) {
  output$results <-  renderTable({
    ipt = fromJSON(input$myDateInterval)
    ret = mydf[mydf$Months %in% ipt,]
    ret
  })
}
shinyApp(ui = ui, server = server)

这篇关于在 Shiny 应用程序中过滤数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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