【发布时间】:2018-06-28 10:18:31
【问题描述】:
我想构建一个非常简单的功能,用户选择要处理的文件,然后基于文件的完整内容的不同输出显示在多个菜单选项卡的各种 data.tables 中。此外,我希望用户能够按从textInput 字段获取的所有输出中存在的值过滤所有这些data.tables。因此,它应该遵循以下逻辑:如果textInput 为空,则显示所有记录,否则仅显示与来自textInput 的值匹配的记录。容易,对吧?
这是一个虚拟示例:
### ui.R
library(shiny)
library(shinydashboard)
library(DT)
dbSidebar <- dashboardSidebar(
sidebarMenu(
textInput(inputId = "search_term",
label = "Search"),
# one of many tabs
menuItem("General Info", tabName = "general_info", icon = icon("info-sign", lib = "glyphicon")),
downloadButton('downloadData', 'Download',
icon("paper-plane"),
style="color: #fff; background-color: #337ab7; border-color: #2e6da4;
margin-top: 20px; margin-left: 15px;")
)
)
general_info_tab <- tabItem(tabName = "general_info",
fluidRow(
box(h2("Companies House Search Data"),
DT::dataTableOutput('searchTable')
),
width = 12)
)
dashboardPage(
dashboardHeader(),
dbSidebar,
dashboardBody(
tabItems(
general_info_tab
)
)
)
### server.R
library(shiny)
library(shinydashboard)
library(DT)
library(dplyr)
# my hypothetical file content
dummy_data <- data.frame(
a = rep(c(1,2,3), 6),
b = rep(c("A2746", "38504", "CD759")),
fruit = rep(c("apple", "pear"), each = 9)
)
shinyServer(function(input, output, session) {
output$searchTable = DT::renderDataTable({
selected <- dummy_data
if(!is.null(input$search_term) ||
!is.na(input$search_term) ||
length(input$search_term) > 0 ||
input$search_term != "") {
selected <- filter(dummy_data, b == input$search_term)
}
DT::datatable(selected)
})
})
使用此代码,我看到空数据集除非我有 textInput 值。如何在默认情况下使其显示为完整数据集(当textInput 字段为空时)并在我编写文本时将其过滤掉?谢谢
【问题讨论】:
标签: r filter shiny reactive-programming shinydashboard