Help needed with basic shiny app

shiny
shiny-error

#1

Hi!

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"))
library(pwr)
library(shiny)

ui <- fluidPage(
  
   titlePanel("Sample Size Calculator - Proportions"),
   sidebarLayout(
     sidebarPanel(
            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),
            br(),
            br(),
            actionButton("button", "Calculate sample size")
     ),
     mainPanel(
            tags$h4("Your required sample size per group:"),
            verbatimTextOutput(outputId="samplesize")
     )
            
  ))

server <- funtion(input, output) {
  eventReactive(input$button,{
      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!


#2

Does it start working if you change

server <- funtion(...

to

server <- function(...


#3

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 <- 
    eventReactive(input$button,{
    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.


#4

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


#5

@nutterb

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