我正在尝试使用用户输入过滤数据框作为单选按钮。不幸的是,只有一种类型的过滤器工作(我的例子中的“年度”版本),但“每月”和“季度”选项不返回任何内容。这是我的示例数据集和代码。
# 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)
答案 0 :(得分:2)
关于此限制的文档不是很清楚,但在
中https://blog.rstudio.com/2017/04/05/shiny-1-0-1/
找到choiceValues中的元素仍然必须是纯文本(这些是 用于计算的值)。但是choiceNames中的元素(UI 标签)可以用HTML构建,使用HTML() 函数或HTML标记生成函数,如标记$ img()和 图标()。
纯文本是必需的,因为它必须越过JS和R之间的边界。您可以使用JSON作为传输器;我不是真的推荐它,但它很容易:
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)
答案 1 :(得分:0)
这对你有用吗?
ui <- fluidPage(
# Input() function
radioButtons(inputId = "myDateInterval", label = "Select Date Interval",
choiceNames = list("Monthly","Quarterly","Annual"), choiceValues = list("Monthly","Quarterly","Annual")),
# Output() functions
tableOutput("results"))
# set up server object
server <- function(input, output) {
output$results <- renderTable({
if(input$myDateInterval == "Monthly") {
mydf2 <- mydf %>% filter(Months %in% (unique(as.character(mydf$Month))))
}
if(input$myDateInterval == "Quarterly") {
mydf2 <- mydf %>% filter(Months %in% (unique(as.character(mydf$Month)))[seq(1,length(unique(mydf$Month)),3)])
}
if(input$myDateInterval == "Annual") {
mydf2 <- mydf %>% filter(Months %in% (unique(as.character(mydf$Month)[1])))
}
mydf2
})
}
shinyApp(ui = ui, server = server)