Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果在R应用程序中使用else_R_If Statement_Shiny - Fatal编程技术网

如果在R应用程序中使用else

如果在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

我有一个简单的样本大小计算器闪亮的应用程序,我试图建立,我似乎无法理解

输入包括1个数字输入和一系列单选按钮。 我正试图根据选择的radiop按钮进行不同的计算

   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})