【问题标题】:R Shiny Reactive SelectInputR闪亮的反应选择输入
【发布时间】:2020-08-01 14:10:57
【问题描述】:

大家好,

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

 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)

【问题讨论】:

    标签: r shiny reactive


    【解决方案1】:

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

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

    编辑 1output$result 被修改以显示匹配 input$numcars 的行数。换句话说,如果您从新的input$numcars selectInput 中进行选择,您将看到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()})
    
    }
    

    【讨论】:

    • 您好本,非常感谢您的帮助。我很高兴 selectInput 现在是反应式的,但是是否也可以更新出现次数?例如,如果我在第一个 checkboxgroupInput 中选中“No”,如果我在第三个 checkboxgroupInput 中选中“Perhaps”,我可以看到表格中有两行,并且在 selecInput 中有两个选项:“1”和“3”。如果我选择“3”,我希望在屏幕右侧将出现次数更新为“1”而不是“2”。我希望我很清楚。非常感谢。
    • 你好 Ben,对不起,我的信息不完整,谢谢,它几乎是完美的:如果在 selectInput 中没有选择,我只想让 textOutput 显示 df1() 的 nrow 的结果.如果在 selectInput 中进行了选择,则会显示 sum(df1()[["NB_CARS"]] == input$numcars) 的结果。因此,如果别无选择,我想在 selectInput 中添加值“All”以考虑所有值,但列表中出现“4”,我不知道它来自哪里。此外,当我们在 selectinput 中做出选择时,是否可以更新 renderTable ?非常感谢您的帮助。
    • 还有一件事:df1() 是什么类型的对象?我没有成功用“assign”将它保存到工作区中。谢谢
    • 非常感谢 Ben,非常完美。我询问如何将 df1() 保存为工作区中的 data.frame 以了解如何保存此类对象,因为我尝试使用命令“assign(df1, df1(), envir = .GlobalEnv)”但它没有工作。稍后,我想对这个data.frame进行其他处理。再次感谢您。
    • 如果你希望df1() 的结果在应用程序终止后仍然存在,那么你可以在你的observe 中执行df1_stored &lt;&lt;- df1()。如果你想在你的server 函数的其他地方访问df1(),你只需调用df1()。或者,您可以创建reactiveValues() 并在global.R 中定义将使对象可用于闪亮应用程序中的所有.R 文件。推荐阅读是scoping rules in shiny
    猜你喜欢
    • 2020-07-04
    • 2017-11-24
    • 2018-10-09
    • 2021-07-16
    • 1970-01-01
    • 2020-08-18
    • 1970-01-01
    • 2016-03-16
    相关资源
    最近更新 更多