繁体   English   中英

R Shiny 反应式选择输入

[英]R Shiny Reactive SelectInput

大家好,

我需要你的帮助:我有一个名为 data_test 的 dataframe,你可以用这些行重现它:

 PC<-c('001','002','003','004','005','006','007','008','009','010')
 A<-c('2','2','1','1','2','0','2','0','1','0')
 B<-c('0','2','0','0','1','0','0','0','1','0')
 C<-c('1','0','0','0','2','2','0','0','0','1')
 NB_CARS<-c('1','3','1','1','0','2','1','3','2','2')
 data_test <- data.frame(PC, A, B, C, NB_CARS)

我有三个链接的 chekboxGroupInputs,名为“variable1”、“variable2”和“variable3”。 当我检查其中一些 chekboxGroupInputs 时,匹配的行数会更新。 但我想插入一个反应式 SelectInput 来显示基于“NB_CARS”变量的汽车数量,但我无法做到这一点。 请你帮助我好吗? 非常感谢。

这是我的代码:

 ui <- fluidPage(
   fluidRow(
     column(3,
            checkboxGroupInput("variable1", "Occurences of column A :",
                               c("Yes (1)" = "1",
                                 "No (2)" = "2",
                                 "Perhaps (0)" = "0")),
            checkboxGroupInput("variable2", "Occurences of column B :",
                               c("Yes (1)" = "1",
                                 "No (2)" = "2",
                                 "Perhaps (0)" = "0")),
            checkboxGroupInput("variable3", "Occurences of column C :",
                               c("Yes (1)" = "1",
                                 "No (2)" = "2",
                                 "Perhaps (0)" = "0"))),

     column(3,
            tableOutput("data")),

     column(3,
            textOutput("result"))
   )
 )

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

   result <- 0

   df1 <- reactive({
     filter(data_test,
            (is.null(input$variable1) | A %in% input$variable1),
            (is.null(input$variable2) | B %in% input$variable2),
            (is.null(input$variable3) | C %in% input$variable3))
   })

   output$result <- renderText(paste("Number of occurences = ", nrow(df1())))


   output$data <- renderTable({df1()})

 }

 shinyApp(ui, server)

我不确定这是您的想法,但也许这会有所帮助(如果没有,请告诉我)。

您可以在您的ui中添加一个selectInput ,该 selectInput 将由您的server中的updateSelectInput更新。

编辑 1 : 修改output$result以显示匹配input$numcars的行数。 换句话说,如果您从新的input$numcars selectInput中选择 select,您将看到NB_CARS匹配所做选择的次数。

编辑 2 :如果您希望将All添加为选项,请注意NB_CARS是一个因素。 因此,您获得了具有意外结果的因子水平(例如 4)。 您可以在与All组合之前使用as.character以获得所需的结果。

此外,当您呈现文本时,只需检查输入是否为All - 如果是,则显示行数。 如果不是,则显示NB_CARS匹配项。 请参阅已编辑的server

ui <- fluidPage(
  fluidRow(
    column(3,
           checkboxGroupInput("variable1", "Occurences of column A :",
                              c("Yes (1)" = "1",
                                "No (2)" = "2",
                                "Perhaps (0)" = "0")),
           checkboxGroupInput("variable2", "Occurences of column B :",
                              c("Yes (1)" = "1",
                                "No (2)" = "2",
                                "Perhaps (0)" = "0")),
           checkboxGroupInput("variable3", "Occurences of column C :",
                              c("Yes (1)" = "1",
                                "No (2)" = "2",
                                "Perhaps (0)" = "0")),
           selectInput("numcars", "Number of Cars", choices = NULL)),
    column(3,
           tableOutput("data")),

    column(3,
           textOutput("result"))
  )
)

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

  result <- 0

  df1 <- reactive({
    filter(data_test,
           (is.null(input$variable1) | A %in% input$variable1),
           (is.null(input$variable2) | B %in% input$variable2),
           (is.null(input$variable3) | C %in% input$variable3))
  })

  observe({
    updateSelectInput(session, "numcars", choices = c("All", unique(as.character(df1()[["NB_CARS"]]))))
  })

  output$result <- renderText({
    if (input$numcars == "All") {
      num_text <- nrow(df1())
    } else {
      num_text <- sum(df1()[["NB_CARS"]] == input$numcars)
    }
    paste("Number of occurences = ", num_text)
  })

  output$data <- renderTable({df1()})

}

暂无
暂无

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

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