我正在创建一个样本大小和功效计算器的版本。运行代码后,选择选项“样本大小”时,在滑动滑块或其他数字输入后立即对样本大小进行任何更新。但是,对于选项“ Power”(均来自第一个“ selectInput”选项),更新根本就不会立即进行。
我不确定正确的前进方向是否是使用反应式表达式。
library(shiny)
library(pwr)
ui <- fluidPage(
titlePanel("Sample Size and Power Calculator"),
sidebarLayout(
sidebarPanel(
#What are we calculating?
selectInput(inputId = "type",
label = strong("Calculator Options"),
choices = c("Pick an Option",
"Power",
"Sample Size"),
selected = "Pick an Option"),
#Display only if Sample Size is selected, ask for necessary parameters
conditionalPanel(
condition = "input.type == 'Sample Size'",
#1. Standard Deviation
numericInput(inputId = "stddev",
label = "Standard Deviation",
value = 10,
min = 1,
max = 400,
step = 1),
#2. Power
sliderInput(inputId = "power",
label = "Power of Study",
min = 0.5,
max = 0.99,
value = 0.8,
step = 0.01),
h6("Power as a decimal not percentage."),
h6("(0.8 means 80% power)"),
#3. Alpha (Significance)
sliderInput(inputId = "alpha",
label = "Significance of Study",
min = 0.01,
max = 0.2,
value = 0.05,
step = 0.01),
h6("Significance of study is most traditionally 0.05 (5%)"),
#4. Meandiff (Difference Between Groups)
numericInput(inputId = "meandiff",
label = "Expected Difference Between Group Means",
value = 20),
#5. Alternative Test
selectInput(inputId = "alt",
label = strong("Alternative Test Options"),
choices = c("Two-Sided" = "two.sided",
"Upper" = "greater",
"Lower" = "less"),
selected = "Two-Sided"),
h6("If unsure, leave at 'Two-Sided'.")
),
#Display only if Power is selected, ask for necessary parameters
conditionalPanel(
condition = "input.type == 'Power'",
#1. Standard Deviation
numericInput(inputId = "stddev",
label = "Standard Deviation",
value = 10,
min = 1,
max = 400,
step = 1),
#2. Size Per Group
numericInput(inputId = "npergroup",
label = "Number per Group",
value = 120),
h6("Assuming equal number in each group, enter number for ONE group."),
#3. Alpha (Significance)
sliderInput(inputId = "alpha",
label = "Significance of Study",
min = 0.01,
max = 0.2,
value = 0.05,
step = 0.01),
h6("Significance of study is most traditionally 0.05 (5%)"),
#4. Meandiff (Difference Between Groups)
numericInput(inputId = "meandiff",
label = "Expected Difference Between Group Means",
value = 20),
#5. Alternative Test
selectInput(inputId = "alt",
label = strong("Alternative Test Options"),
choices = c("Two-Sided" = "two.sided",
"Upper" = "greater",
"Lower" = "less"),
selected = "Two-Sided"),
h6("If unsure, leave at 'Two-Sided'.")
)
),
#Output:
mainPanel(
textOutput(outputId = "intro"),
textOutput(outputId = "desc"),
conditionalPanel(
condition = "input.type == 'Sample Size'",
textOutput(outputId = "samplesize")
),
conditionalPanel(
condition = "input.type == 'Power'",
textOutput(outputId = "power")
)
)
)
)
server <- function(input, output) {
#Introductory Text
output$intro <- renderText({
"Select an option and adjust the sliders and parameters."
})
#Description of what has been chosen
output$desc <- renderText({
paste("You chose: ", input$type)
})
#If Sample Size is selected, what is the sample size
output$samplesize <- renderText({
paste("Sample Size Per Group for Two-Sample t-test for Mean Diff
Assuming Two Groups and Equal Variances: ",
as.character(ceiling(pwr.t.test(d = input$meandiff / input$stddev,
sig.level = input$alpha,
power = input$power,
type = "two.sample",
alternative = input$alt)$n)))
})
#If Power is selected, what is the power
output$power <- renderText({
paste("Power for Two-Sample t-test for Mean Diff Assuming Two Groups and
Equal Variances: ",
format(round(pwr.t.test(d = input$meandiff / input$stddev,
sig.level = input$alpha,
n = input$npergroup,
type = "two.sample",
alternative = input$alt)$power * 100, 2),
nsmall = 2))
})
}
shinyApp(ui = ui, server = server)
答案 0 :(得分:0)
所有输入ID都是唯一的将是理想的。在下面,我为这些重复的输入ID添加了2
。所有这些都在功率分析中。更改之后,代码即可工作。
library(shiny)
library(pwr)
ui <- fluidPage(
titlePanel("Sample Size and Power Calculator"),
sidebarLayout(
sidebarPanel(
#What are we calculating?
selectInput(inputId = "type",
label = strong("Calculator Options"),
choices = c("Pick an Option",
"Power",
"Sample Size"),
selected = "Pick an Option"),
#Display only if Sample Size is selected, ask for necessary parameters
conditionalPanel(
condition = "input.type == 'Sample Size'",
#1. Standard Deviation
numericInput(inputId = "stddev",
label = "Standard Deviation",
value = 10,
min = 1,
max = 400,
step = 1),
#2. Power
sliderInput(inputId = "power",
label = "Power of Study",
min = 0.5,
max = 0.99,
value = 0.8,
step = 0.01),
h6("Power as a decimal not percentage."),
h6("(0.8 means 80% power)"),
#3. Alpha (Significance)
sliderInput(inputId = "alpha",
label = "Significance of Study",
min = 0.01,
max = 0.2,
value = 0.05,
step = 0.01),
h6("Significance of study is most traditionally 0.05 (5%)"),
#4. Meandiff (Difference Between Groups)
numericInput(inputId = "meandiff",
label = "Expected Difference Between Group Means",
value = 20),
#5. Alternative Test
selectInput(inputId = "alt",
label = strong("Alternative Test Options"),
choices = c("Two-Sided" = "two.sided",
"Upper" = "greater",
"Lower" = "less"),
selected = "Two-Sided"),
h6("If unsure, leave at 'Two-Sided'.")
),
#Display only if Power is selected, ask for necessary parameters
conditionalPanel(
condition = "input.type == 'Power'",
#1. Standard Deviation
numericInput(inputId = "stddev2",
label = "Standard Deviation",
value = 10,
min = 1,
max = 400,
step = 1),
#2. Size Per Group
numericInput(inputId = "npergroup",
label = "Number per Group",
value = 120),
h6("Assuming equal number in each group, enter number for ONE group."),
#3. Alpha (Significance)
sliderInput(inputId = "alpha2",
label = "Significance of Study",
min = 0.01,
max = 0.2,
value = 0.05,
step = 0.01),
h6("Significance of study is most traditionally 0.05 (5%)"),
#4. Meandiff (Difference Between Groups)
numericInput(inputId = "meandiff2",
label = "Expected Difference Between Group Means",
value = 20),
#5. Alternative Test
selectInput(inputId = "alt2",
label = strong("Alternative Test Options"),
choices = c("Two-Sided" = "two.sided",
"Upper" = "greater",
"Lower" = "less"),
selected = "Two-Sided"),
h6("If unsure, leave at 'Two-Sided'.")
)
),
#Output:
mainPanel(
textOutput(outputId = "intro"),
textOutput(outputId = "desc"),
conditionalPanel(
condition = "input.type == 'Sample Size'",
textOutput(outputId = "samplesize")
),
conditionalPanel(
condition = "input.type == 'Power'",
textOutput(outputId = "power")
)
)
)
)
server <- function(input, output) {
#Introductory Text
output$intro <- renderText({
"Select an option and adjust the sliders and parameters."
})
#Description of what has been chosen
output$desc <- renderText({
paste("You chose: ", input$type)
})
#If Sample Size is selected, what is the sample size
output$samplesize <- renderText({
paste("Sample Size Per Group for Two-Sample t-test for Mean Diff
Assuming Two Groups and Equal Variances: ",
as.character(ceiling(pwr.t.test(d = input$meandiff / input$stddev,
sig.level = input$alpha,
power = input$power,
type = "two.sample",
alternative = input$alt)$n)))
})
#If Power is selected, what is the power
output$power <- renderText({
paste("Power for Two-Sample t-test for Mean Diff Assuming Two Groups and
Equal Variances: ",
format(round(pwr.t.test(d = input$meandiff2 / input$stddev2,
sig.level = input$alpha2,
n = input$npergroup,
type = "two.sample",
alternative = input$alt2)$power * 100, 2),
nsmall = 2))
})
}
shinyApp(ui = ui, server = server)