简体   繁体   English

使用checkboxGroupInput过滤数据框

[英]Filter data frame using checkboxGroupInput

I want to create a scatterplot using R-Shiny App. 我想使用R-Shiny App创建一个散点图。 I need 2 input (selectInput and checkboxGroupInput) to display my plot. 我需要2个输入(selectInput和checkboxGroupInput)来显示我的绘图。 When I run the code, it show this error : 当我运行代码时,它显示此错误:

Error: (converted from warning) Error in : (converted from warning) Error in max: (converted from warning) no non-missing arguments to max; 错误:(从警告转换)错误:(从警告转换)最大错误:(从警告转换)没有不可缺少的参数传递给max; returning -Inf 返回-Inf

It seems that selectInput is OK but not checkboxGroupInput, because when I try to filter data with 2 selectInput, it works... See my code : 似乎selectInput可以,但checkboxGroupInput不能,因为当我尝试使用2 selectInput过滤数据时,它可以工作...请参阅我的代码:

ui = fluidPage(
  headerPanel('Title'),

  sidebarPanel(
    selectInput(inputId = 'adv', label = 'Adversaire', choices = levels(nodes$Adversaire)),
    checkboxGroupInput(inputId = 'act', label = 'Actions', choices = levels(nodes$Action))
  ),
  mainPanel(plotOutput('scatter'))
)

server <- function(input, output) {
  data = reactive({
    df = nodes %>%
      filter(Adversaire == input$adv, Action %in% c(input$act)) %>%
      group_by(Player) %>%
      summarise(Poste = unique(Poste),
                Pour_brut = sum(Pour), Contre_brut = sum(Contre), Total_brut = sum(Total),
                Pour = sum(Pour_brut)/mean(unique(Time))*20, Contre = sum(Contre_brut)/mean(unique(Time))*20, Total = sum(Total)/mean(unique(Time))*20,
                Time = mean(unique(Time)))
  })

  output$scatter = renderPlot({
    ggplot(data(), aes(x = Contre, y = Pour, color = Poste, size = Time)) +
      scale_x_continuous(limits = c(0,max(c(data()$Contre, data()$Pour)))) +
      scale_y_continuous(limits = c(0,max(c(data()$Contre, data()$Pour)))) +
      geom_abline(intercept = 0, slope=1) +
      geom_point()
  })      
}
shinyApp(ui = ui, server = server)

EDIT : 编辑:

structure(list(Player = c(14L, 12L, 96L, 25L, 19L, 96L), Poste = structure(c(2L, 
1L, 1L, 2L, 1L, 1L), .Label = c("Attaquant", "Defenseur"), class = "factor"), 
    Match = c(2L, 2L, 2L, 2L, 2L, 2L), Adversaire = structure(c(2L, 
    2L, 2L, 2L, 2L, 2L), .Label = c("Amiens", "Nice"), class = "factor"), 
    Action = structure(c(3L, 3L, 3L, 2L, 2L, 2L), .Label = c("But", 
    "FO", "JOZO", "PK 4vs5", "PP 5vs3", "PP 5vs4", "SZC", "SZSPR", 
    "TOZD", "TOZN", "TOZO"), class = "factor"), Pour = c(2L, 
    2L, 2L, 2L, 1L, 1L), Contre = c(1L, 2L, 1L, 0L, 0L, 0L), 
    Total = c(3L, 4L, 3L, 2L, 1L, 1L), Time = c(12.89, 11.33, 
    11.11, 14.42, 10.12, 11.11)), row.names = c(NA, 6L), class = "data.frame")

This worked fine for me. 这对我来说很好。 Just make sure Adversaire and Action are character fields. 只要确保AdversaireAction是字符字段即可。 Also added req(nrow(data()) > 0) to renderPlot() to get rid of max function warnings - req(nrow(data()) > 0)renderPlot()以摆脱max功能警告-

ui = fluidPage(
  headerPanel('Title'),

  sidebarPanel(
    selectInput(inputId = 'adv', label = 'Adversaire', choices = unique(nodes$Adversaire)),
    checkboxGroupInput(inputId = 'act', label = 'Actions', choices = unique(nodes$Action))
  ),
  mainPanel(plotOutput('scatter'))
)

server <- function(input, output) {
  data <- reactive({
    df <- nodes %>%
      filter(Adversaire == input$adv, Action %in% input$act) %>%
      group_by(Player) %>%
      summarise(
        Poste = unique(Poste),
        Pour_brut = sum(Pour), Contre_brut = sum(Contre),
        Total_brut = sum(Total),
        Pour = sum(Pour_brut)/mean(unique(Time))*20,
        Contre = sum(Contre_brut)/mean(unique(Time))*20,
        Total = sum(Total)/mean(unique(Time))*20,
        Time = mean(unique(Time))
      )
    df
  })

  output$scatter = renderPlot({
    req(nrow(data()) > 0)
    ggplot(data(), aes(x = Contre, y = Pour, color = Poste, size = Time)) +
      scale_x_continuous(limits = c(0,max(c(data()$Contre, data()$Pour)))) +
      scale_y_continuous(limits = c(0,max(c(data()$Contre, data()$Pour)))) +
      geom_abline(intercept = 0, slope=1) +
      geom_point()
  })      
}
shinyApp(ui = ui, server = server)

在此处输入图片说明

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

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