【发布时间】:2018-01-30 16:10:09
【问题描述】:
我在 Shiny 应用程序反应函数中使用 dplyr。我在 UI 中有一个交互式小部件,用户可以使用它来选择状态,并且将根据所选状态显示数据。不过,我还想提供显示所有内容的选项,这是我称之为“全部”的状态。
我知道如何在没有 dplyr 的情况下实现这一点:
library(tibble)
library(shiny)
# Test Data -----
test_data <- tribble(
~id, ~status,
1, 'active',
2, 'inactive',
3, 'test'
)
# Ui ----
ui = fluidPage(
selectInput("status", "Choose a status:",
choices = list("All","active","inactive","test")
),
tableOutput('result')
)
# Server ----
server = function(input, output) {
table <- reactive({
if(input$status != 'All')
{
test_data <- test_data[test_data$status == input$status,]
} else {
test_data
}
})
output$result <- renderTable(table(), rownames = FALSE)
}
# App ---
shinyApp(ui = ui, server = server)
但是,我想与我的其余代码保持一致并使用 dplyr。有没有办法做类似下面的事情?
library(tibble)
library(dplyr)
library(shiny)
# Test Data -----
test_data <- tribble(
~id, ~status,
1, 'active',
2, 'inactive',
3, 'test'
)
# Ui ----
ui = fluidPage(
selectInput("status", "Choose a status:",
choices = list("All","active","inactive","test")
),
tableOutput('result')
)
# Server ----
server = function(input, output) {
table <- reactive({
test_data %>%
filter(ifelse(input$status != 'All', status == input$status, status == ***pass***))
})
output$result <- renderTable(table(), rownames = FALSE)
}
# App ---
shinyApp(ui = ui, server = server)
换句话说,有没有办法在过滤器函数中使用 ifelse 在特定条件下不过滤?
谢谢!
【问题讨论】:
标签: r shiny dplyr reactive magrittr