【问题标题】:R Shiny: Displays all tests at once after checking the checkboxesR Shiny:选中复选框后立即显示所有测试
【发布时间】:2021-09-06 20:15:07
【问题描述】:

我正在尝试制作一个 R Shiny App,它会在选中复选框后同时打印 3 个统计测试。 问题是应用程序只打印一个输出,而其他输出“未创建”。Like can be seen here

我试过了

if(input$Stattest=='PP' | input$Stattest==1)

但是..它不起作用。

我的代码是:

 output$Stattest = renderUI({
     checkboxGroupInput("Stattest", "Choose test for stationarity", choices = c("ADF","PP","KPSS-TREND"))
  })
adf <- reactive({
    if(is.na(input$Stattest)){
      return(NULL)}

     if(input$Stattest=='ADF'){
       d=list()
       c=list(df_sel()[[print(input$y)]],df_sel()[[print(input$x1)]],df_sel()[[print(input$x2)]])
       for (i in 1:3){
         d[[i]]=adf.test(c[[i]])
       }
           }
    d
    })

  output$ADF=renderPrint({
    adf()
  })

 pp <- reactive({

    if(is.na(input$Stattest)){
      return(NULL)}

    if(input$Stattest=='PP' | input$Stattest==1){
      ptest=list()
      d=list(df_sel()[[print(input$y)]],df_sel()[[print(input$x1)]],df_sel()[[print(input$x2)]])
      for (i in 1:3){
        ptest[[i]]=pp.test(d[[i]])
      }

    }
    ptest
  })


  output$PP=renderPrint({
    pp()
  })

主要的“问题”是我需要创建这个 reactive 函数,因为所有这些测试都将打印在 pdf 报告中。 知道我怎样才能让它工作吗?

提前致谢。

【问题讨论】:

    标签: r checkbox shiny printing output


    【解决方案1】:

    制作c 和相关的“测试变量名称”反应式表达式,执行所需的测试,将所有内容拉入最终列表,然后根据用户输入显示到 UI:

    library(shiny)
    
    ui <- fluidPage(
      uiOutput(outputId = "Stattest"), 
      
      verbatimTextOutput(outputId = "test_result")
    )
    
    server <- function(input, output, session) {
      output$Stattest = renderUI({
        checkboxGroupInput(
          inputId = "stat_test", 
          label = "Stat test:", 
          choices = c("ADF","PP","KPSS-TREND")
        )
      })
      
      # Since `df_sel()` is reactive, so should `c` be: 
      
      # <-- adf.test -->
      cc <- reactive({
        list(df_sel()[[input$y]], df_sel()[[input$x1]], 
             df_sel()[[input$x2]]) |> 
          lapply(FUN = adf.test)
      })
      
      # <-- pp.test -->
      dd <- reactive({
        list(df_sel()[[input$y]], df_sel()[[input$x1]], 
              df_sel()[[input$x2]]) |> 
          lapply(FUN = pp.test)
      })
      
      # <-- kpss-trend -->
      # ee <- reactive({
      #   list(subset_as_required) |> 
      #     lapply(FUN = your_kpss_trend_function)
      # })
      
      # <-- Final results -->
      all_results <- reactive({
        list(ADF = cc(), PP = dd(), "KPSS-TREND" = ee())
      })
      
      output$test_result <- renderPrint({
        for (test in input$stat_test) {
          print(all_results()[test])
        }
      })
    }
    
    shinyApp(ui, server)
    
    

    【讨论】:

    • 您好,非常感谢!问题是如果我使用selectInput(),在 pdf-report 中它将只打印选择的测试(例如,如果 ADF 在 selectInput 中,则在报告中只打印 ADF)并且我不能调用 cc()直接在报告中。我尝试创建一个名为 test_stat 的反应函数,然后在 output$ 中打印所有内容,但在这种情况下,我无法调用cc()。我该怎么办?
    • 我已对答案进行了编辑。看看吧。
    • 再次感谢您!但是....我觉得我解释的不是很好,对不起....所以主要的想法是我需要在pdf-report上打印ADF和PP测试(和KPSS),但是使用这个开关功能, 只有在selectInput 中选择的功能才会打印在报告中。即使在应用程序中测试将一次打印一个,在报告中我需要同时打印所有三个。很抱歉造成这种误解。
    • 否 :( 错误是Error in [: object of type 'closure' is not subsettable。我什至尝试使用[[
    • 已更正。已经把 all_results 而不是 all_results()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-17
    • 1970-01-01
    • 2015-09-05
    • 1970-01-01
    • 2013-06-21
    • 2014-09-27
    • 1970-01-01
    相关资源
    最近更新 更多