Help needed with basic shiny app




I'm new to programming shiny apps. I did work with R a bit before for clinical research.

Can anyone please help me with getting this code running? I'm trying to build a simple sample size calculator for the comparison of 2 independent proportions, using a function from the pwr package.

The function is pwr.2p.test(h = , n = NULL, sig.level = , power = , alternative = ("two.sided","greater", "less"))
with h <- ES.h(p1, p2) calculating Cohen's effect size (2*asin(sqrt(p1))-2*asin(sqrt(p2)))
n is the number of patients required per group - this is what I'm after.
I would like the user to be able to adjust the other variables in a reactive way (see my input code).

This is what I've come up with:

install.packages(c("pwr", "powerSurvEpi"))

ui <- fluidPage(
   titlePanel("Sample Size Calculator - Proportions"),
            radioButtons(inputId="power", "Power level:", c("0.80", "0.90"), selected = "0.80"),
            radioButtons(inputId="significance", "Significance level/Alpha:", c("0.01", "0.05", "0.10"), selected = "0.05"),
            radioButtons(inputId="type", "Two or one-sided test:", c("two.sided", "greater", "less"), selected = "two.sided"),
            numericInput(inputId="p1", "Proportion group 1", value=0.00, min=0, max = 1, step=0.10),
            numericInput(inputId="p2", "Proportion group 2", value=0.00, min=0, max = 1, step=0.10),
            actionButton("button", "Calculate sample size")
            tags$h4("Your required sample size per group:"),

server <- funtion(input, output) {
      h <- ES.h(input$p1, input$p2)
      result <- pwr.2p.test(h=as.numeric(h), n=NULL, sig.level=as.numeric(input$significance), power=as.numeric(input$power), alternative=input$type)
  output$samplesize <- renderPrint({result[2]})

shinyApp(ui = ui, server = server)

I did try to get it working in many different ways, but I get some error messages in the console:

  • Error: unexpected '{' in "server <- funtion(input, output) {"
  • Error in output$samplesize <- renderText({ : object 'output' not found

And I simply do not get the app to show the calculated n in the output frame.

Anyone? A million thanks in advance!


Does it start working if you change

server <- funtion(...


server <- function(...


One other small change. Instead of defining result inside of the eventReactive call, you want result to be the output of that call.

server <- function(input, output) {
  result <- 
    h <- ES.h(input$p1, input$p2)
    pwr.2p.test(h = as.numeric(h), 
                n = NULL, 
                sig.level = as.numeric(input$significance), 
                power = as.numeric(input$power), 
                alternative = input$type)
  output$samplesize <- renderPrint({result()[2]})

Then in output$samplesize, you should use

output$samplesize <- renderPrint({result()[2]})

(notice the parentheses after result). It seems to work for me at this point.


Thanks nutterb! Your answer makes sense, and it does work now.



With your help, I was able to extend the sample size calculator.
In case you're curious, this is the end result:

GCR Sample Size Calculator