筛选和调整数据 table
Filtering and adjusting data table
我正在尝试显示具有多个 pickerInput
变量的数据框。当按一个变量过滤时,在下面的示例中,物种,它工作正常。但是,我似乎无法弄清楚如何在尝试按秒(最终是第三个变量)过滤数据帧时格式化子设置代码
library(shiny)
library(data.table)
results <- iris
results$Species <- as.character(results$Species)
# UI
ui <- fluidPage(
# Application title
titlePanel(
h1("Iris Table", align="center")
),
fluidRow(
# column(3,
# pickerInput("sepal_width_input", "Sepal", choices = results$Sepal.Width, options = list(`actions-box` = TRUE), selected=NULL, multiple=TRUE)),
column(3,
pickerInput("speciesInput", "Species", choices=unique(results$Species), options = list(`actions-box` = TRUE), selected=NULL, multiple=TRUE)
),
column(9,
DT::dataTableOutput('table')))
)
# Server
server <- function(input, output) {
mydata <- reactive({
if (is.null(input$speciesInput)) {df <- results
} else df <- results[results$Species %in% input$speciesInput,]
df
})
output$table <- DT::renderDataTable(
datatable(mydata())
)
}
# Run the application
shinyApp(ui = ui, server = server)
我在主题标签后面添加了第二个 pickerInput。有人能告诉我在包含这两个变量时应该如何格式化以下代码片段吗?
mydata <- reactive({
if (is.null(input$speciesInput)) {df <- results
} else df <- results[results$Species %in% input$speciesInput,]
df
})
您可以编写一个处理过滤的函数,并将输入传递给该函数,如下所示:
library(shiny)
library(shinyWidgets)
library(DT)
results <- iris
results$Species <- as.character(results$Species)
filter_data <- function(spec=NULL, sepw=NULL) {
res <- results
if(!is.null(spec)) res <- res %>% dplyr::filter(Species %in% spec)
if(!is.null(sepw)) res <- res %>% dplyr::filter(Sepal.Width %in% sepw)
return(res)
}
# UI
ui <- fluidPage(
# Application title
titlePanel(
h1("Iris Table", align="center")
),
fluidRow(
column(3,
pickerInput("sepal_width_input", "SepalWidth", choices = results$Sepal.Width, options = list(`actions-box` = TRUE), selected=NULL, multiple=TRUE)),
column(3,
pickerInput("speciesInput", "Species", choices=unique(results$Species), options = list(`actions-box` = TRUE), selected=NULL, multiple=TRUE)
),
column(9,
DT::dataTableOutput('table')))
)
# Server
server <- function(input, output) {
mydata <- reactive({
filter_data(input$speciesInput, input$sepal_width_input)
})
output$table <- DT::renderDataTable(
datatable(mydata())
)
}
# Run the application
shinyApp(ui = ui, server = server)
我正在尝试显示具有多个 pickerInput
变量的数据框。当按一个变量过滤时,在下面的示例中,物种,它工作正常。但是,我似乎无法弄清楚如何在尝试按秒(最终是第三个变量)过滤数据帧时格式化子设置代码
library(shiny)
library(data.table)
results <- iris
results$Species <- as.character(results$Species)
# UI
ui <- fluidPage(
# Application title
titlePanel(
h1("Iris Table", align="center")
),
fluidRow(
# column(3,
# pickerInput("sepal_width_input", "Sepal", choices = results$Sepal.Width, options = list(`actions-box` = TRUE), selected=NULL, multiple=TRUE)),
column(3,
pickerInput("speciesInput", "Species", choices=unique(results$Species), options = list(`actions-box` = TRUE), selected=NULL, multiple=TRUE)
),
column(9,
DT::dataTableOutput('table')))
)
# Server
server <- function(input, output) {
mydata <- reactive({
if (is.null(input$speciesInput)) {df <- results
} else df <- results[results$Species %in% input$speciesInput,]
df
})
output$table <- DT::renderDataTable(
datatable(mydata())
)
}
# Run the application
shinyApp(ui = ui, server = server)
我在主题标签后面添加了第二个 pickerInput。有人能告诉我在包含这两个变量时应该如何格式化以下代码片段吗?
mydata <- reactive({
if (is.null(input$speciesInput)) {df <- results
} else df <- results[results$Species %in% input$speciesInput,]
df
})
您可以编写一个处理过滤的函数,并将输入传递给该函数,如下所示:
library(shiny)
library(shinyWidgets)
library(DT)
results <- iris
results$Species <- as.character(results$Species)
filter_data <- function(spec=NULL, sepw=NULL) {
res <- results
if(!is.null(spec)) res <- res %>% dplyr::filter(Species %in% spec)
if(!is.null(sepw)) res <- res %>% dplyr::filter(Sepal.Width %in% sepw)
return(res)
}
# UI
ui <- fluidPage(
# Application title
titlePanel(
h1("Iris Table", align="center")
),
fluidRow(
column(3,
pickerInput("sepal_width_input", "SepalWidth", choices = results$Sepal.Width, options = list(`actions-box` = TRUE), selected=NULL, multiple=TRUE)),
column(3,
pickerInput("speciesInput", "Species", choices=unique(results$Species), options = list(`actions-box` = TRUE), selected=NULL, multiple=TRUE)
),
column(9,
DT::dataTableOutput('table')))
)
# Server
server <- function(input, output) {
mydata <- reactive({
filter_data(input$speciesInput, input$sepal_width_input)
})
output$table <- DT::renderDataTable(
datatable(mydata())
)
}
# Run the application
shinyApp(ui = ui, server = server)