简体   繁体   English

通过下拉菜单选择数据行

[英]Choosing rows of data by a drop-down menu

This code reads in the data, find unique values of a column ( Location ) and puts these values as options in the dropdown menu. 该代码读取数据,查找列的唯一值( Location ),并将这些值作为选项放入下拉菜单中。 My goal is to customize my data based on values that are chosen in the dropdown menu. 我的目标是根据在下拉菜单中选择的值来自定义数据。 My data looks like below: 我的数据如下所示:

我的资料

I tried to view the data but I found it is not working properly. 我试图查看数据,但发现它无法正常工作。 What should I do? 我该怎么办?

Update 1: The problem is in data()$Location == input$Locationscheck but I don't know how to fix it. 更新1:问题出在data()$Location == input$Locationscheck但我不知道如何解决。

library(shiny)

dropdownButton <- function(label = "", status = c("default", "primary", "success", "info", "warning", "danger"), ..., width = NULL) {

  status <- match.arg(status)
  # dropdown button content
  html_ul <- list(
    class = "dropdown-menu",
    style = if (!is.null(width)) 
      paste0("width: ", validateCssUnit(width), ";"),
    lapply(X = list(...), FUN = tags$li, style = "margin-left: 10px; margin-right: 10px;")
  )
  # dropdown button apparence
  html_button <- list(
    class = paste0("btn btn-", status," dropdown-toggle"),
    type = "button", 
    `data-toggle` = "dropdown"
  )
  html_button <- c(html_button, list(label))
  html_button <- c(html_button, list(tags$span(class = "caret")))
  # final result
  tags$div(
    class = "dropdown",
    do.call(tags$button, html_button),
    do.call(tags$ul, html_ul),
    tags$script(
      "$('.dropdown-menu').click(function(e) {
      e.stopPropagation();
});")
  )
}

ui <- fluidPage(
  fileInput(inputId = "uploadedcsv","", accept = '.csv'),
  dropdownButton(label = "Choose the locations",status = "default",
                 width = 250,actionButton(inputId = "allLocations", label = "(Un)select all"),
                 checkboxGroupInput(inputId = "Locationscheck",label = "Choose",choices = "")),
  actionButton('Run', label = "Run!")
)

server <- function(input, output, session) {

  data <- reactive({
    infile <- input$uploadedcsv

    if (is.null(infile))
      return(NULL)

    read.csv(infile$datapath, header = TRUE, sep = ",")
  })

  observe({
    locationnames <- unique(data()$Location)
    updateCheckboxGroupInput(session, "Locationscheck",
                             choices = locationnames,
                             selected = locationnames)

    ### selecting and de-selecting in step 2 ###
    observeEvent(input$allLocations, {
      if (is.null(input$Locationscheck)) {
        updateCheckboxGroupInput(session = session,
                                 inputId = "Locationscheck",
                                 selected = locationnames)
      } else {
        updateCheckboxGroupInput(session = session,
                                 inputId = "Locationscheck",
                                 selected = "")
      }
    })
    ### End of selecting and de-selecting ###

    observeEvent(input$Run, {
      Newdata <- data()[data()$Location == input$Locationscheck,]
      View(data())
      View(Newdata)
    })

  })
}
shinyApp(ui = ui, server = server)

The problem in the code data()$Location == input$Locationscheck is that it only considers first element in the input$Locationscheck vector and gives you the result as the values that are matched(eg: Location1) . 代码data()$Location == input$Locationscheck在于,它仅考虑input$Locationscheck向量中的第一个元素,并为您提供结果作为匹配的值(例如Location1)。 You should use which(data()[data()$Location %in% input$Locationscheck,]) instead. 您应该改用which(data()[data()$Location %in% input$Locationscheck,]) which gives the indexes and %in% compares data()$Location with all the values in the input$Locationscheck vector. which给出索引,并且%in%data()$Locationinput$Locationscheck向量中的所有值进行比较。

I have modified your code and further added a table to illustrate the working: 我已经修改了您的代码,并进一步添加了表格来说明其工作原理:

library(shiny)

 dropdownButton <- function(label = "", status = c("default", "primary", "success", "info", "warning", "danger"), ..., width = NULL) {

   status <- match.arg(status)
   # dropdown button content
   html_ul <- list(
     class = "dropdown-menu",
     style = if (!is.null(width)) 
       paste0("width: ", validateCssUnit(width), ";"),
     lapply(X = list(...), FUN = tags$li, style = "margin-left: 10px; margin-right: 10px;")
   )
   # dropdown button apparence
   html_button <- list(
     class = paste0("btn btn-", status," dropdown-toggle"),
     type = "button", 
     `data-toggle` = "dropdown"
   )
   html_button <- c(html_button, list(label))
   html_button <- c(html_button, list(tags$span(class = "caret")))
   # final result
   tags$div(
     class = "dropdown",
     do.call(tags$button, html_button),
     do.call(tags$ul, html_ul),
     tags$script(
       "$('.dropdown-menu').click(function(e) {
       e.stopPropagation();
 });")
  )
   }

 ui <- fluidPage(
   fileInput(inputId = "uploadedcsv","", accept = '.csv'),
   dropdownButton(label = "Choose the locations",status = "default",
                  width = 250,actionButton(inputId = "allLocations", label = "(Un)select all"),
                  checkboxGroupInput(inputId = "Locationscheck",label = "Choose",choices = "")),
   actionButton('Run', label = "Run!"),
   tableOutput('table')
 )

 server <- function(input, output, session) {

   data <- reactive({
     infile <- input$uploadedcsv

     if (is.null(infile))
       return(NULL)

     read.csv(infile$datapath, header = TRUE, sep = ",", stringsAsFactors = FALSE)
   })

   observe({
     locationnames <- unique(data()$Location)

     updateCheckboxGroupInput(session, "Locationscheck",
                              choices = locationnames,
                              selected = locationnames)

     ### selecting and de-selecting in step 2 ###
     observeEvent(input$allLocations, {
       if (is.null(input$Locationscheck)) {
         updateCheckboxGroupInput(session = session,
                                  inputId = "Locationscheck",
                                  selected = locationnames)
       } else {
         updateCheckboxGroupInput(session = session,
                                  inputId = "Locationscheck",
                                  selected = "")
       }
     })
     ### End of selecting and de-selecting ###

     observeEvent(input$Run, {
       # Newdata <- data()[data()$Location == input$Locationscheck,]
       Newdata <- data()[which(data()$Location %in% input$Locationscheck),]
       # # View(data())
       # View(Newdata)
       output$table <- renderTable({
         Newdata
       })

     })

   })
 }
 shinyApp(ui = ui, server = server)

I suggest you use isolate when you access the value so that the reactive event is not triggered again and again, something like this Newdata <- isolate(data())[which(isolate(data())$Location %in% input$Locationscheck),] 我建议您在访问该值时使用isolate ,这样就不会一次又一次触发反应性事件,例如: Newdata <- isolate(data())[which(isolate(data())$Location %in% input$Locationscheck),]

Hope it helps! 希望能帮助到你!

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

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