提问人:Diana Horvath 提问时间:10/25/2023 更新时间:10/25/2023 访问量:33
闪亮的应用程序 - 使用条件面板根据输入调整输出
Shiny app - adjust output based on input using conditional panels
问:
我对 Shiny 应用程序和 R 非常陌生,希望有任何建议!我正在创建一个应用程序,用于执行计算并根据多个输入输出绘图。现在,我在条件面板(复选框)中有一个名为“Clustered”的输入,当用户选择它时,其他一些输入将可用。目前,当选择“集群”复选框时,我的输出(图)会根据新输入(ICC、cluster_size)正确更改。但是,我遇到的问题是,当用户尝试更改两个条件面板中使用的输入值时,它不会更新。
我的 UI 代码如下所示:
ui <- fluidPage(
# Font ----
tags$head(
tags$style(HTML("
h2 {
font-family: Century Gothic, sans-serif;
}"))
),
# App title ----
titlePanel(div("XXXXXXX", style = "color: #E35925")),
# Top panel -----
fluidRow(
column(3, # size of box
selectInput("alpha",
"Significance Level",
c("Alpha = 0.01",
"Alpha = 0.05",
"Alpha = 0.10"),
selected = "Alpha = 0.05")
),
column(3, numericInput( 'power', 'Power', .8, min = 0.1, max = 1, step = .1))
),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
tags$head(
tags$style(HTML("
body {
font-family: Century Gothic, sans-serif;
font-size: 10pt;
font-weight: bolder;
}"))
),
# Input: Binary outcome -------
checkboxInput(inputId = "binary",
label = "Binary Outcome",
value = FALSE,
width = NULL),
# Input: Clustered design -------
checkboxInput(inputId = "cluster",
label = "Clustered Design",
value = FALSE,
width = NULL),
####### First Panel: No cluster & continous outcome #########
conditionalPanel(
condition ="input.cluster == false & input.binary == false",
# Input: Power level -------------
#sliderInput(inputId = "power", label = "Power:", min = 0, max = 1, value = .8),step = ) if you want to specify step size
# Input: min and max MDE ----------
numericInput('MDE_min', 'Minimum Detectable Effect (min)', .1, min = 0, max = 10),
numericInput('MDE_max', 'Minimum Detectable Effect (max)', .4, min = 0, max = 10),
# numericInput('MDE_scale', 'Minimum Detectable Effect (scale)', .02, min = 0, max = 10),
#numericInput('propT', 'Proportion in Treatment', .5, min = 0, max = 1)
# Input: Standard Deviation -------------
numericInput('SD',
'Standard Deviation of the Outcome',
.4, min = 0,
max = 1000000,
step = .1),
# Input: Proportion in Treatment ---
sliderInput(inputId = "propT",
label = "Proportion in Treatment",
min = 0,
max = 1,
value = .5)
), # First conditional panel ends ------------------------------------
# Second panel: Clustered and continous outcome ----------
conditionalPanel(
condition ="input.cluster == true & input.binary == false",
# Inputs:
numericInput('MDE_min', 'Minimum Detectable Effect (min)', .1, min = 0, max = 10),
numericInput('MDE_max', 'Minimum Detectable Effect (max)', .4, min = 0, max = 10),
numericInput('SD', 'Standard Deviation of the Outcome', .4, min = 0, max = 1000000,
step = .1),
sliderInput(inputId = "propT", label = "Proportion in Treatment",
min = 0, max = 1, value = .5),
sliderInput(inputId = "ICC", label = "Intracluster Correlation",
min = 0, max = 1, value = .2),
numericInput('cluster_size', 'Cluster Size', 20, min = 0, max = 1000000,
step = 1),
)
),
# Main panel for displaying outputs ----
mainPanel(
# Output: Power Plot ----
plotOutput(outputId = "powerPlot")
)
)
)
从我已经查找的内容来看,这可能是由于我的输入具有相同的 ID?在选择“集群”后,我如何使用这些相同的输入 ID,同时让两个面板中的相互输入更新。从我所读到的内容来看,反应式表达可能会有所帮助——但我尝试了这些,但我不太确定我做错了什么。我将不胜感激任何建议!
以下是我的其余代码:
server <- function(input, output) {
output$powerPlot <- renderPlot({
#------- Create function - No Clusters --------#
SS_function = function(MDE, propT, SD){7.84*((SD^2)/(((propT*(1-propT))*(MDE*MDE))))}
MDE = seq(input$MDE_min, input$MDE_max, by=((input$MDE_max-input$MDE_min)*.05))
propT = input$propT
SD = input$SD
sample_size=SS_function(MDE, propT, SD)
#------- Create function - With Clusters ------#
SS_function_cl = function(MDE, propT, SD, cluster_size, ICC)
{ (SD^2*(1+(cluster_size-1))*ICC) /
(cluster_size * (MDE/2.802)^2 * (propT*(1-propT)))
}
MDE = seq(input$MDE_min, input$MDE_max, by=((input$MDE_max-input$MDE_min)*.05))
propT = input$propT
SD = input$SD
ICC = input$ICC
cluster_size = input$cluster_size
N_clusters = SS_function_cl(MDE, propT, SD, cluster_size, ICC)
sample_size_cl= cluster_size * N_clusters # should be times 2?
#----------- Create plot ------------#
if (input$cluster %in% "FALSE") {
data <- data.frame(MDE,sample_size)
data %>% ggplot(aes(x=sample_size, y=MDE)) +
geom_line(aes(color="E35925")) +
geom_point(aes(color="E35925")) +
theme(legend.position="none",
axis.text=element_text(size=12),
axis.title=element_text(size=14),
text = element_text(family = "Century Gothic")) +
labs(y= "MDE", x = "Sample Size")
} else {
data <- data.frame(MDE,sample_size_cl)
data %>% ggplot(aes(x=sample_size_cl, y=MDE)) +
geom_line(aes(color="E35925")) +
geom_point(aes(color="E35925")) +
theme(legend.position="none",
axis.text=element_text(size=12),
axis.title=element_text(size=14),
text = element_text(family = "Century Gothic")) +
labs(y= "MDE", x = "Sample Size")
}
}) # output ends
} # server function ends
######################################
# Create Shiny app #
######################################
shinyApp(ui = ui, server = server)
我尝试添加反应式表达式,但很难让它们正常运行。
答: 暂无答案
评论
conditionalPanels
conditionalPanel