【问题标题】:using else if in a R shiny app在 R 闪亮的应用程序中使用 else if
【发布时间】: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


【解决方案1】:

您的问题其实很简单:查看inputId 中的radioButtons

radioButtons(
  inputId = "'QC_Type'",
  label = "QC Type",
  choices = c(
    "Overall" = 1,
    "Solicited" =2,
    "Spontaneous" = 3,
    "Clinical Trial"=4,
    "Literature"= 5
  ) 
)

只需删除 QC_Type 周围的额外引号,它就可以正常工作了。

另外:当 Shiny 谈到反应性表达时,它不仅仅意味着 reactive。名称中带有reactiveobserverender 的任何内容都将计算在内。所以不需要这条线:

x <- reactive({input$QC_Type})

由于您已经在反应式表达式中(renderText),您可以删除该行并在您当前拥有x() 的任何地方使用input$QC_Type

【讨论】:

  • 非常感谢!!!效果很好 - 可能只是问如何将输出限制为小数点后 2 位?
  • 查看?round 了解可以通过不同方式执行此操作的一堆函数
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-23
  • 2016-12-20
  • 1970-01-01
  • 1970-01-01
  • 2020-06-16
  • 2021-09-21
  • 2016-05-27
相关资源
最近更新 更多