简体   繁体   中英

RShiny reactive error

I am building a simple RShiny App that calculates sample size and power, but I keep getting this error message--- Warning: Error in .getReactiveEnvironment()$currentContext: Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.) I couldn't figure out how to fix it. This is my first time using RShiny. If anyone can help, I really appreciate that! Thanks a lot!

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)

I don't think it is required to have reactive for final. try this below. it works for me, except for pwr.2p.test , looks like that is some function you are trying to use. Also, I did not understand why you had HTML(final) , use of renderUi should generate html by default. Let me know how did it go. Good luck

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
       )}
  })
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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