[英]Using two dependent selectInput to filter a dataframe in R Shiny
G'day awesome community G'day 真棒社区
I am trying to make a dashboard of a dataframe that allows one to filter the dataframe by the levels within a column selected.我正在尝试制作 dataframe 的仪表板,该仪表板允许通过所选列中的级别过滤 dataframe。 This means a first
pickerInput
where the user selects the column, and then a second child pickerInput
where the options are generated based on the column selected.这意味着用户选择列的第一个
pickerInput
,然后是根据所选列生成选项的第二个子pickerInput
。 I have figured out one way to make the pickerInput
s dependent on each other, but for some reason when I try to apply the filtering, my dataframe has zero values and I cant see why?我已经找到了一种使
pickerInput
相互依赖的方法,但是由于某种原因,当我尝试应用过滤时,我的 dataframe 的值为零,我不明白为什么?
Please see the reprex created with the mtcars dataset请查看使用 mtcars 数据集创建的代表
library(shiny)
library(shinyWidgets)
library(dplyr)
library(DT)
data(mtcars)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(uiOutput('select_filter'),
uiOutput('filter')),
mainPanel(
dataTableOutput('table')
),
))
server <- function(input, output, session) {
data<-mtcars
categories<-c('cyl','vs','am','gear','carb')
output$select_filter <- renderUI({
pickerInput("select_filter", "Select flexi filter",
choices = levels(as.factor(categories))
)})
output$filter <- renderUI({
pickerInput("filter", "Flexi filter",
choices = unique(data[,input$select_filter]),
options = list('actions-box'=TRUE), multiple= TRUE,
selected = unique(data[,input$select_filter]))
})
filtered_data<-
#
reactive ({data %>% filter(input$select_filter %in% input$filter)
})
output$table<-renderDataTable(filtered_data())
}
shinyApp(ui, server)
Any help will be greatly appreciated.任何帮助将不胜感激。 If any further information is required please let me know.
如果需要任何进一步的信息,请告诉我。 Cheers
干杯
In filter
, use .data
to get the column value using select_filter
variable.在
filter
中,使用.data
使用select_filter
变量获取列值。 Also included req
so that it doesn't error out at the start when the input$select_filter
is NULL
.还包括
req
以便当input$select_filter
为NULL
时它不会在开始时出错。
filtered_data <-
reactive ({
req(input$select_filter)
data %>% filter(.data[[input$select_filter]] %in% input$filter)
})
Complete app code -完整的应用程序代码 -
library(shiny)
library(shinyWidgets)
library(dplyr)
library(DT)
data(mtcars)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(uiOutput('select_filter'),
uiOutput('filter')),
mainPanel(
dataTableOutput('table')
),
))
server <- function(input, output, session) {
data<-mtcars
categories<-c('cyl','vs','am','gear','carb')
output$select_filter <- renderUI({
pickerInput("select_filter", "Select flexi filter",
choices = levels(as.factor(categories))
)})
output$filter <- renderUI({
pickerInput("filter", "Flexi filter",
choices = unique(data[,input$select_filter]),
options = list('actions-box'=TRUE), multiple= TRUE,
selected = unique(data[,input$select_filter]))
})
filtered_data<-
#
reactive ({
req(input$select_filter)
data %>% filter(.data[[input$select_filter]] %in% input$filter)
})
output$table<-renderDataTable(filtered_data())
}
shinyApp(ui, server)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.