我正在构建一个简单的RShiny应用程序,该应用程序可以计算样本大小和功效,但是我不断收到此错误消息-
警告:.getReactiveEnvironment()$ currentContext中的错误:如果没有 Activity 的反应性上下文,则不允许进行操作。 (您尝试做只能从反应式表达式或观察器内部完成的操作。)
我不知道如何解决它。这是我第一次使用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)

最佳答案

我认为最终不必有任何反应。请在下面尝试。
它对我有用,除了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
       )}
  })
}

关于r - R Shiny 无功误差,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41291668/

10-14 16:02