【发布时间】:2018-10-25 02:27:45
【问题描述】:
我有一个简单的样本大小计算器闪亮的应用程序,我正在尝试构建我似乎无法弄清楚的。
输入由 1 个数字输入和一系列单选按钮组成。 我试图根据选择的单选按钮进行不同的计算
ui <- shinyUI(fluidPage(
titlePanel("alpha version of sample size calculator"),
sidebarPanel(
numericInput(
"expn",
"Please enter total number of reports received",
1,
min = 0,
max = 1000000
),
radioButtons(
inputId = "'QC_Type'",
label = "QC Type",
choices = c(
"Overall" = "a",
"Solicited" ="b",
"Spontaneous" = "c",
"Clinical Trial"="d",
"Literature"= "e"
)
)
),
mainPanel (
p ("Your required sample size is"),
textOutput("results"))
))
server <- function (input, output) {
A1 <- (1.96*1.96)*(0.3)*(1-0.3)/(0.05*0.05)
B1 <- (1.96*1.96)*(0.32)*(1-0.32)/(0.05*0.05)
C1 <- (1.96*1.96)*(0.35)*(1-0.35)/(0.05*0.05)
D1 <- (1.96*1.96)*(0.26)*(1-0.26)/(0.05*0.05)
E1 <- (1.96*1.96)*(0.36)*(1-0.36)/(0.05*0.05)
options(digits=2)
if (input$QC_Type=="a") {
output$results <- renderText({
as.numeric( (input$expn)*(A1)/(input$expn+(A1)+1))
})
} else if (input$QC_Type=="b") {
output$results <- renderText({
as.numeric( (input$expn)*(B1)/(input$expn+(B1)+1))
})
} else if (input$QC_Type=="c") {
output$results <- renderText({
as.numeric( (input$expn)*(C1)/(input$expn+(C1)+1))
})
} else if (input$QC_Type=="d") {
output$results <- renderText({
as.numeric( (input$expn)*(D1)/(input$expn+(D1)+1))
})
} else if (input$QC_Type=="e") {
output$results <- renderText({
as.numeric( (input$expn)*(E1)/(input$expn+(E1)+1))
})
}
}
但是,这不会运行,并会产生提及反应元素的错误消息。
更新:
感谢您的回复,R 社区令人难以置信 :) - 我确实看过建议的线程,而我确实注意到我的代码尝试基于数字输入和单选按钮执行计算的相似性。我尝试了以下在渲染函数中包含逻辑的方法:
server <- function (input, output) {
options(digits=2)
output$results <- renderText({
x <- reactive({input$QC_Type})
A1 <- (1.96*1.96)*(0.3)*(1-0.3)/(0.05*0.05)
B1 <- (1.96*1.96)*(0.32)*(1-0.32)/(0.05*0.05)
C1 <- (1.96*1.96)*(0.35)*(1-0.35)/(0.05*0.05)
D1 <- (1.96*1.96)*(0.26)*(1-0.26)/(0.05*0.05)
E1 <- (1.96*1.96)*(0.36)*(1-0.36)/(0.05*0.05)
if (x()=="a") {
as.numeric( (input$expn)*(A1)/(input$expn+(A1)+1))
} else if (x()=="b") {
as.numeric( (input$expn)*(B1)/(input$expn+(B1)+1))
} else if (x()=="c") {
as.numeric( (input$expn)*(C1)/(input$expn+(C1)+1))
} else if (x()=="d") {
as.numeric( (input$expn)*(D1)/(input$expn+(D1)+1))
} else if (x()=="e") {
as.numeric( (input$expn)*(E1)/(input$expn+(E1)+1))
}
}
)
}
应用程序运行,但在主输出窗格中,我收到一条消息,指出参数的长度为 0,而不是所需的计算。
更新 2
经过进一步挖掘,我意识到“零长度”是由于语法中缺少组件(req 输入)我的代码当前如下,但是现在没有生成输出 - 任何进一步的指导将不胜感激。
library(shiny)
ui <- shinyUI(fluidPage(
titlePanel("alpha version of sample size calculator"),
sidebarPanel(
numericInput(
"expn",
"Please enter total number of reports received",
1,
min = 0,
max = 1000000
),
radioButtons(
inputId = "'QC_Type'",
label = "QC Type",
choices = c(
"Overall" = 1,
"Solicited" =2,
"Spontaneous" = 3,
"Clinical Trial"=4,
"Literature"= 5
)
)
),
mainPanel (
p ("Your required sample size is"),
textOutput("results"))
))
## server attempt 5
server <- function (input, output,session) {
options(digits=2)
A1 <- (1.96*1.96)*(0.3)*(1-0.3)/(0.05*0.05)
B1 <- (1.96*1.96)*(0.32)*(1-0.32)/(0.05*0.05)
C1 <- (1.96*1.96)*(0.35)*(1-0.35)/(0.05*0.05)
D1 <- (1.96*1.96)*(0.26)*(1-0.26)/(0.05*0.05)
E1 <- (1.96*1.96)*(0.36)*(1-0.36)/(0.05*0.05)
output$results <- renderText({
x <- reactive({input$QC_Type})
req(input$QC_Type)
req(input$expn)
if (x()==1) {
as.character( (input$expn)*(A1)/(input$expn+(A1)+1))
} else if (x()==2) {
as.character( (input$expn)*(B1)/(input$expn+(B1)+1))
} else if (x()==3) {
as.character( (input$expn)*(C1)/(input$expn+(C1)+1))
} else if (x()==4) {
as.character( (input$expn)*(D1)/(input$expn+(D1)+1))
} else if (x()==5) {
as.character( (input$expn)*(E1)/(input$expn+(E1)+1))
}
}
)
}
shinyApp(ui = ui, server= server)
【问题讨论】:
-
首先,尝试在你的代码中使用CTRL-A、CTRL-I,这样我们更容易理解。
-
嗨 Teodor,感谢您的评论 - 我只是按照您的建议进行操作,代码显示方式没有任何变化。请接受我对格式的道歉 - 我对此有点陌生:)
-
关于您的问题,您不能在
reactive()的render_函数之外使用input$QC_Type(任何输入) -
你需要在 reactive() 上搜索更多。您的问题是重复的,因此您的问题的答案可以在 Stack 上找到。 stackoverflow.com/questions/31639947/…
标签: r if-statement shiny