繁体   English   中英

通过多列中的值有效过滤R Shiny中的数据框

[英]Filter dataframe efficiently in R shiny by values in several columns

我想知道一种有效的方法来进行以下操作。 在闪亮的应用中具有反应式dataframe() 我想有两个反应式输入(每个输入有2个可能性TRUEFALSE ),它们分别基于两列中的值来对行进行子集化。 如果我只有一个输入(和一列photos ),我会执行以下操作:

df<-reactive({
  df<-mydf
  if(input$myinput==FALSE)
  {
    df<-df[!df$photos=="",]
  }
  else{
    df
  }
}) 

问题是,如果我有两个(或更多个)的输入(和列),如果我使用嵌套的代码将增长太多ifelse内部的ifelse在上面的例子中,以允许两个4种可能性TRUE/FALSE投入。

编辑:可重现,使第二个输入工作没有太多的ifelse

server <- function(input, output, session) { 
  df<-reactive({
    df<-iris
    if(input$Petalw==T)
    {
      df<-df[df$Petal.Width==0.2,]
    }
    else{
      df
    }
  }) 
  output$table <- DT::renderDataTable(
    DT::datatable(df(), options = list(searching = FALSE,pageLength = 25))
  )
}
ui <- navbarPage(
  title = 'Select values in two columns based on two inputs respectively',
  fluidRow(
    column(width = 3,
           checkboxInput("Petalw","PetalWithIs0.2",T),
           checkboxInput("PetalL","PetalLengthis1.4",T)
    ),
    column(9,
  tabPanel('Table',       DT::dataTableOutput('table'))
  )
  )
)
shinyApp(ui, server) 

您可以通过input[[inputName]]访问输入,其中inputName是输入的名称(例如“ Sepal.Length-7.9”)。 然后您可以通过检查所有输入

if(input[[inputName]]){
   split <- strsplit(inputName, "-")[[1]]
   name <- split[1]
   treshold <- as.numeric(split[2])
   global$filter[, inputName ==colnames(filter)] <- iris[name] == treshold
}else{
   global$filter[, inputName ==colnames(filter)] = TRUE
}

您可以使用renderUI()创建的输入:

output$checkBoxes <- renderUI({
    lapply(inputNames, function(inputName) checkboxInput(inputName, inputName, FALSE))
  })

在示例中,我使用所有数字列的最大值。

完整代码如下:

restr <- apply(iris, 2, max)[1:4]
inputNames <- paste(names(restr), restr, sep = "-") 
filter = sapply(inputNames, function(inputName) c(inputName = return(rep(TRUE, dim(iris)[1]))))


server <- function(input, output, session) { 
  global <- reactiveValues(filter = filter)

  df <- reactive({
      for(inputName in inputNames){
        if(!is.null(input[[inputName]])){
          isolate({
            if(input[[inputName]]){
              split <- strsplit(inputName, "-")[[1]]
              name <- split[1]
              treshold <- as.numeric(split[2])
              global$filter[, inputName ==colnames(filter)] <- iris[name] == treshold
            }else{
              global$filter[, inputName ==colnames(filter)] = TRUE
            }
          })
        }
      }
      iris[rowSums(global$filter) == 4, ]
    })


  output$checkBoxes <- renderUI({
    lapply(inputNames, function(inputName) checkboxInput(inputName, inputName, FALSE))
  })

  output$table <- DT::renderDataTable(
    DT::datatable(df(), options = list(searching = FALSE,pageLength = 25))
  )
}
ui <- navbarPage(
  title = 'Select values in two columns based on two inputs respectively',
  fluidRow(
    column(width = 3,
           uiOutput("checkBoxes")
    ),
    column(9,
           tabPanel('Table', DT::dataTableOutput('table'))
    )
  )
)
shinyApp(ui, server) 

您可以让用户为某一列选择一个值,然后基于该值对数据进行子集处理,然后使用renderUI并使用其他列中的值生成动态selectInput下拉列表。

server <- function(input, output, session) { 
  df <- reactive({
    subset(iris, Petal.Width == input$Petalw)
  })

  # Extract list of Petal Lengths from selected data - to be used as a filter
  p.lengths <- reactive({
    unique(df()$Petal.Length)
  })

  # Filter based on Petal Length
  output$PetalL <- renderUI({
    selectInput("PetalLengthSelector", "PetalLength", as.list(p.lengths()))
  })

  # Subset this data based on the values selected by user
  df_1 <- reactive({
    foo <- subset(df(), Petal.Length == input$PetalLengthSelector)
    return(foo)
  })

  output$table <- DT::renderDataTable(
    DT::datatable(df_1(), options = list(searching = FALSE,pageLength = 25))
  )
}
ui <- navbarPage(
  title = 'Select values in two columns based on two inputs respectively',
  fluidRow(
    column(width = 3,
           selectInput("Petalw","PetalWidth", choices = unique(iris$Petal.Width)),
           uiOutput("PetalL")
    ),
    column(9,
           tabPanel('Table', DT::dataTableOutput('table'))
    )
  )
)
shinyApp(ui, server)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM