使用checkboxGroupInput过滤数据框

时间:2018-11-02 11:11:03

标签: r checkbox shiny

我想使用R-Shiny App创建一个散点图。我需要2个输入(selectInput和checkboxGroupInput)来显示我的绘图。当我运行代码时,它显示此错误:

  

错误:(从警告转换)错误::(从警告转换)   最大错误:(从警告转换)没有不可缺少的参数   最大返回-Inf

似乎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)

编辑:

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")

1 个答案:

答案 0 :(得分:0)

这对我来说很好。只要确保AdversaireAction是字符字段即可。还向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)

enter image description here