【问题标题】:How can I subset my dataframe based on the input of a checkbox in a shiny app?如何根据闪亮应用程序中复选框的输入对我的数据框进行子集化?
【发布时间】:2019-10-03 22:21:21
【问题描述】:

我正在开发一个闪亮的应用程序,我正在尝试根据复选框的输入过滤数据框,如果单击复选框,数据框将被过滤,否则数据框保持不变。如果选中该复选框,则必须在“CANONICAL”列中选择值为“YES”的行

我已经阅读过类似的问题(例如:Condition Filter in dplyr based on Shiny input),但我无法解决我的问题。

我的代码如下:

library(shiny)
library(DT) 
library(dplyr)
library(shinyWidgets)

options(shiny.maxRequestSize = 100*1024^2)

ui <- fluidPage(

  sidebarLayout(

    sidebarPanel(
      fileInput("file1", "Upload your File",
                multiple = FALSE,
                accept = c("text/csv",
                           "text/comma-separated-values,text/plain",
                           ".csv")),

      pickerInput("Consequence", "Consequence:",   
                  choices = c( "x", "y","z"), 
                  options = list(`actions-box` = TRUE),
                  multiple = TRUE ),
      prettyCheckbox(inputId= "CANONICAL", label = "CANONICAL", value =              FALSE, outline= TRUE, fill = TRUE, bigger = TRUE, status = 'success',width = NULL),

    mainPanel(
      dataTableOutput("contents")
    )

  ))

server <- function(input, output) {

  output$contents <- renderDT({
    req(input$file1)
    df <- read.delim(input$file1$datapath,
                     header = TRUE,
                     sep = '\t') 

    # Apply filters 
    df <- df %>% 
    filter(Consequence == input$Consequence ) %>%
    { if (input$CANONICAL) filter( CANONICAL == 'YES') }

return(df)

shinyApp(ui, server) 

如果条件为(input$CANONICAL)(input$CANONICAL == TRUE)(!is.null(input$CANONICAL)),则最后一个过滤器不起作用。我也尝试在单独的反应函数中定义条件,但似乎没有任何效果。

【问题讨论】:

    标签: r user-interface shiny dplyr


    【解决方案1】:

    请下次尝试使用干净的 r 代码添加可重现的示例。

    在当前形式中,您不能在管道运算符之后使用 if 条件。

    我通常在过滤语句中添加 if 条件。请参阅下面的代码。在这种情况下,您必须提供 else 条件,否则代码将无法工作。

    请注意,我已更改您的示例以使其可重现。

    library("shiny")
    library(DT)
    library(dplyr)
    library(shinyWidgets)
    
    ## Only run examples in interactive R sessions
    
    shinyApp(
    
        # options(shiny.maxRequestSize = 100*1024^2)
    
        ui = fluidPage(
    
            sidebarLayout(
    
                sidebarPanel(
    
                    pickerInput("Consequence", "Consequence:",   
                                choices = c( "x", "y","z"), 
                                options = list(`actions-box` = TRUE),
                                selected = "x",
                                multiple = TRUE ),
    
                    prettyCheckbox(inputId = "CANONICAL",
                                   label = "CANONICAL",
                                   value = FALSE,
                                   outline = TRUE,
                                   fill = TRUE,
                                   bigger = TRUE,
                                   status = 'success',width = NULL)),
    
    
                mainPanel(
                    dataTableOutput("contents")
                )
    
                )
            ),
    
            server = function(input, output) {
    
                df <- data.frame(Consequence = c(rep("x",4),rep("y",4),rep("z",4)),
                              CANONICAL = rep(c("YES","NO"),6),
                                 x1 = c(5,6,7,3,4,5,2,3,4,2,1,7),
                                 x2 = c(1,2,3,2,3,2,1,4,6,7,3,4),
                                 x3 = c(12,43,64,34,93,16,32,74,84,89,45,67)
                              )
    
    
                output$contents <- renderDT({
    
                    print(input$CANONICAL)
    
                df <- df %>% 
                    filter(Consequence == input$Consequence ) %>%
                    filter(if (input$CANONICAL == TRUE) CANONICAL == "YES" else !is.na(CANONICAL))
    
                return(df)
                })
            }
    )
    

    【讨论】:

    • 非常感谢,这真的很有用。
    猜你喜欢
    • 2016-10-01
    • 2019-12-28
    • 1970-01-01
    • 1970-01-01
    • 2014-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多