在 Shiny 应用程序中过滤数据框

编程入门 行业动态 更新时间:2024-10-25 21:28:02
本文介绍了在 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

blog.rstudio/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 应用程序中过滤数据框

本文发布于:2023-10-23 13:41:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1520978.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   数据   Shiny

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!