使用R Shiny中的用户输入过滤数据表输出

克里希

我正在开发一个允许用户选择特定输入的应用程序。在这种情况下,该应用程序提供了两个selectizeInput选项以从各种选项中进行选择。

以下是数据集:

data_test = data.frame(Name = c ("ABC","ABC","ABC","DEF","DEF", "XYZ", "XYZ", "PQR"),
          Country = c("US, Japan","US, Japan","US, Japan","Canada, US","Canada, US", "UK, US", "UK, US", "Germany"),
          Region = c("North America, Asia","North America, Asia","North America, Asia","North America","North America", "Europe, North America", "Europe, North America", "Europe"),
          Contact = c(1234,1234,1234,7578,7578,9898,9898,7660),
          ContactPerson = c("Geoff","Mary","Mike","Don","Sean","Jessica","Justin","John"))

在ui

dashboardPage(skin = "blue",
  dashboardHeader(title = 'My APP'),

  dashboardSidebar(
    sidebarMenu(
      menuItem("Profiles", tabName = "profiles", icon=icon("user")),
      menuItem("Search", tabName = "search", icon=icon("search")),
      menuItem("About App", tabName="about", icon = icon("info"))
    )
  ),
  dashboardBody(
    tabItems(
            tabItem(tabName ="profiles",
                    tabBox( title = "", 
                            width = 12, id = "tabset1", height = "850px",
                    tabPanel("People",
                            fluidRow(
                          box(title = "Filters", solidHeader = TRUE,
                              background = "blue" , collapsible = TRUE, width = 12,
                                  fluidRow(
                                    column(4,selectizeInput("country",label="Country",choices= NULL, multiple = TRUE)),
                                    column(4,selectizeInput("geogPref",label="Region",choices= NULL, multiple = TRUE))
                                          )
                              )
                            ),
                          box(title = "Filtered Results",
                              collapsible = TRUE, status = "success",
                              width = 12, DT::dataTableOutput('results'))
                            ),
                    tabPanel("Details",
                             fluidRow(
                                    box(width = 4, background = "blue",
                                        collapsible = TRUE, solidHeader = TRUE)
                                    )
                            )
                          )
                    ),
            tabItem(tabName ="search",
                    titlePanel("Search"),

                    fluidRow(
                            )  
                    ),
            tabItem(tabName="about",
                    titlePanel("About APP"),
                    HTML("This is an app.")
                    )
            )
      )
)

在server.R中

library(shiny)
trim.leading <- function (x)  sub("^\\s+", "", x)

uniqueValues <- function(x){
values <- c()
s <- (unlist(strsplit(x, ",", fixed = TRUE)))
v <- trim.leading(s)
}    

geog <- c()
geog <- unique(unlist(c(geog, sapply(data_set$Region, uniqueValues))))


shinyServer(function(input, output, session) {

  updateSelectizeInput(session, 'country', choices = unique(data_set$Country), server = TRUE)
  updateSelectizeInput(session, 'geogPref', choices = geog, server = TRUE)

  country <-  reactive({
    c <- c()
    c <- c(c, input$country)
  })


  dataset <- reactive({
    data <- data_set
    if (input$country){
      data$c1 <- grepl(paste(country(), collapse = "|"), data$Country)
    }
    else {
      data$c1 <- TRUE
    }

    if (input$geogPref){
      data$c2 <- grepl(input$geogPref, data$Region)
    }
    else {
      data$c2 <- TRUE
    }


    data <- data[which(data$c1 == TRUE & data$c2 == TRUE ),c("Name", "Contact", "ContactPerson")]

    return (data)

  })

  output$results <- DT::renderDataTable(
    DT::datatable( unique(dataset()),
                   rownames = FALSE, options = list(searchable = FALSE)
    ) 

})

因此,根据用户选择,我需要过滤出包含所有这些字符串的行,并仅使用那些相关的行来更新表。我无法使用过滤器更新表格。有了这段代码,我得到了这个错误:

Error in if: argument is of length zero
Stack trace (innermost first):
    96: <reactive:dataset> [D:\shinyapps\myapp/server.R#21]
    85: dataset
    84: unique
    83: DT::datatable
    82: exprFunc

有人可以帮我解决我做错的事情吗?

休伯特

您可以简化服务器代码:

shinyServer(function(input, output, session) {

      updateSelectizeInput(session, 'country', choices = unique(data_set$Country), server = TRUE)
      updateSelectizeInput(session, 'geogPref', choices = geog, server = TRUE)

      dataset <- reactive({
        data <- data_set
        if (length(input$country)){
          data$c1 <- grepl(paste(input$country, collapse = "|"), data$Country)
        }
        else {
          data$c1 <- TRUE
        }

        if (length(input$geogPref)){
          data$c2 <- grepl(paste(input$geogPref, collapse = "|"), data$Region)
        }
        else {
          data$c2 <- TRUE
        }

        data[data$c1  & data$c2 ,c("Name", "Contact", "ContactPerson")]
      })

      output$results <- DT::renderDataTable(
        DT::datatable( dataset(),
                       rownames = FALSE, options = list(searchable = FALSE)
        )) 
    })

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何基于R Shiny中的Leaflet映射的输入来过滤数据表?

R Shiny:如何从DT数据表的搜索框中获取用户输入?

基于R Shiny中基于用户输入的表作为输出

更新数据表后,为什么数据表中的R / Shiny输入无法正常工作?

如何使用Shiny中的可编辑数据表作为另一个数据表的输入

DT和Shiny:使用过滤器格式化数据表中的数字

在Shiny中使用过滤后的数据表

从Shiny的navPanel输出的tabPanel中的数据表:如何使用CSS更改背景颜色

R Shiny - 无法根据“selectInput”用户选择在主面板中显示数据表

如何使用闪亮输入过滤已编辑的数据表?

使用来自 Excel 工作表的用户输入过滤数据透视表中的一行

R Shiny:处理数据表中的操作按钮

DT数据表R Shiny中的条件格式

R Shiny,如何使数据表对数据表中的复选框做出反应

使用R Shiny软件包保存插入的数据(在数据表中)

使用数据表来屏蔽/过滤R中的另一个数据表

如何在不破坏“传播”功能的情况下使用 R Shiny 中的复选框过滤反应性数据表?

使用Shiny中的迷你图渲染数据表

使用R Shiny中的操作按钮将格式添加到数据表冲突中

R Shiny:使用复选框更新数据表

如何使用R DT Shiny根据颜色矢量为数据表中的文本着色?

在R Shiny中访问使用CellEdit编辑的更新数据表

使用数组C#中的值过滤数据表

过滤 AdminLTE 中使用的数据表中的列

R数据表-加入但使用更新过滤

当R Shiny DT数据表被排序/过滤时,如何确保行着色更新?

无法发布Shiny R数据表

如何使用 checkboxgroup 输入在 R 中创建一个数据表?

R Shiny中的数据表:借助JavaScript通过KeyTable扩展从数据表中读取标记的单元格