如果在R应用程序中使用else
我有一个简单的样本大小计算器闪亮的应用程序,我试图建立,我似乎无法理解 输入包括1个数字输入和一系列单选按钮。 我正试图根据选择的radiop按钮进行不同的计算如果在R应用程序中使用else,r,if-statement,shiny,R,If Statement,Shiny,我有一个简单的样本大小计算器闪亮的应用程序,我试图建立,我似乎无法理解 输入包括1个数字输入和一系列单选按钮。 我正试图根据选择的radiop按钮进行不同的计算 ui <- shinyUI(fluidPage( titlePanel("alpha version of sample size calculator"), sidebarPanel( numericInput( "expn", "Please enter total number
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))
})
}
}
ui您的问题其实很简单:查看单选按钮的输入
:
radioButtons(
inputId = "'QC_Type'",
label = "QC Type",
choices = c(
"Overall" = 1,
"Solicited" =2,
"Spontaneous" = 3,
"Clinical Trial"=4,
"Literature"= 5
)
)
只需删除QC_Type
周围的额外引号,就可以了
另外:当SHINK谈到反应式表达式时,它不仅仅意味着反应式。名称中任何带有反应性
、观察
或渲染
的内容都将被计算在内。所以不需要这条线:
x <- reactive({input$QC_Type})
x首先,尝试在代码上使用CTRL-A、CTRL-I,让我们更容易理解。嗨,Teodor,谢谢你的评论-我只是按照你的建议做了,代码的显示方式没有任何改变。请接受我关于格式化的歉意-我对此有点陌生:)关于您的问题,您不能在reactive()
函数的render\ucode>之外使用input$QC\u Type
(任何输入)
您需要在reactive()上搜索更多内容。您的问题是重复的,因此可以在堆栈上找到问题的答案。非常感谢你!!!这很好-可能只是询问如何将输出限制在小数点后2位?请查看?round
,了解一系列可以以不同方式实现这一点的函数
radioButtons(
inputId = "'QC_Type'",
label = "QC Type",
choices = c(
"Overall" = 1,
"Solicited" =2,
"Spontaneous" = 3,
"Clinical Trial"=4,
"Literature"= 5
)
)
x <- reactive({input$QC_Type})