RShiny反应性错误

时间:2016-12-22 21:07:51

标签: r error-handling

我正在构建一个简单的RShiny应用程序来计算样本大小和功率,但我不断收到此错误信息--- 警告:.getReactiveEnvironment()中的错误$ currentContext:没有活动的响应上下文时不允许操作。 (你试图做一些只能在反应式表达式或观察者内部完成的事情。) 我无法弄清楚如何解决它。这是我第一次使用RShiny。如果有人可以提供帮助,我真的很感激!非常感谢!

library(shiny)
ui <- fluidPage(
headerPanel("Power and Sample Size Calculator"),
fluidRow(column(12,
              wellPanel(
                helpText("Two proportions (equal sample size in each group) power/sample size analysis"),
                selectInput (inputId = "choice",
                             label = " Please Choose What You Want To Calculate",
                             c("Power","Sample Size"),selected = NULL, 
                             multiple = FALSE,
                             selectize = TRUE, width = NULL, size = NULL)
                )),
       column(4,
              wellPanel(
                conditionalPanel(
                  condition = "input$choice = Power",
                  numericInput (inputId = "tau",
                                label = "Effect Size",
                                value = "0.2",
                                min = 0, max =1),
                  numericInput (inputId = "n", 
                                label = "Sample Size in Each Group", 
                                value = "200",
                                min = 0,
                                max = 100000000),
                  sliderInput (inputId = "alpha", 
                               label = "Significance Level ⍺= ", 
                               value = "0.05",
                               min = 0.001, max = 0.10)),
                conditionalPanel(
                  condition = "input$choice=Sample Size",
                  numericInput (inputId = "tau",
                                label = "Effect Size",
                                value = "0.2",
                                min = 0, max =1),
                  sliderInput (inputId = "alpha", 
                               label = "Significance Level ⍺= ", 
                               value = "0.05",
                               min = 0.001, max = 0.10),
                  numericInput (inputId = "beta", 
                                label = "Power", 
                                value = "0.8",
                                min = 0,
                                max = 1))
                )
                ),
       column(8,
              wellPanel(
                htmlOutput("Result")
              ))
              ))


server <- function(input, output) {
choice <- switch (input$choice,
                "Power" = 1, "Sample Size" = 2)
output$Result <- renderUI({
if(choice==1){
  final=reactive({pwr.2p.test(h = input$tau, n = input$n, sig.level = input$alpha, power = )
  })
}
if(choice==2){
  final=reactive({pwr.2p.test(h = input$tau, n = , sig.level = input$alpha, power = input$beta)
})}
HTML(final)
}
)
}

 shinyApp(ui=ui, server=server)

1 个答案:

答案 0 :(得分:0)

我认为不需要对最终决定进行反应。试试这个。 它适用于我,除了pwr.2p.test,看起来这是你正在尝试使用的一些功能。另外,我不明白为什么你有HTML(final),使用renderUi应该默认生成html。让我知道它是怎么回事。祝你好运

server <- function(input, output) {
  choice <- reactive({
    switch(input$choice,"Power" = 1,"Sample Size" = 2)})

  output$Result <- renderUI({
    if (input$choice == 'Power') {
      pwr.2p.test( h = input$tau,
                   n = input$n,
           sig.level = input$alpha,
               power = input$beta 
        )}
    if (input$choice == 'Sample Size') {
      pwr.2p.test( h = input$tau,
                   n = ,
           sig.level = input$alpha,
               power = input$beta
       )}
  })
}