【问题标题】:Radio buttons with numerical inputs in conditional panels条件面板中带有数字输入的单选按钮
【发布时间】:2017-08-22 23:00:25
【问题描述】:

我正在尝试制作一个闪亮的应用程序来进行计算,如 [this pape 有 3 种计算,称为“calcprior”、“calcpval”和“calcFPR”。

radioButton 选择要进行的计算。每个计算都需要不同的输入。输入被放置在条件面板中。正确的名称出现在条件面板中,但数值不会传递给服务器,例如input$pval 没有在条件面板的 numericInput 中输入的值。

相比之下,所有 3 次计算所需的 nsamp 值被正确传递到服务器。

我是 Shiny 的初学者,所以我希望有人能解释一下出了什么问题。无法看到变量的值使得调试比在常规 R 中困难一百万倍。

    sidebarLayout(

    sidebarPanel(
     radioButtons("calctype", "Choose calculation:",selected = "calcFPR", 
     choices = c(
    "prior (for given FPR and P val)" = "calcprior",
    "P value (for given FPR and prior)" = "calcpval",    
     "FPR (for given P value and prior)" = "calcFPR")),

    conditionalPanel(
     condition = "input.calctype == 'calcprior'",
     numericInput("pval", label = h5("observed P value"), value = 0.05, min 
    = 0, max = 1),
     numericInput("FPR", label = h5("false positive risk"), value = 0.05, 
     min = 0, max = 1)
     ),

    conditionalPanel(
     condition = "input.calctype == 'calcpval'",
      numericInput("FPR", label = h5("false positive risk"), value = 0.05, 
      min = 0, max = 1),
      numericInput("prior", label = h5("prior probability of real effect"), 
     value = 0.5, min       = 0, max = 1)
     ),

    conditionalPanel(
     condition = "input.calctype == 'calcFPR'",
      numericInput("pval",label = h5("observed P value"),value = 0.05, min = 
      0, max = 1),
      numericInput("prior", label = h5("prior probability of real effect"), 
      value = 0.5, min = 0., max = 1.)
      ),

     inputPanel(
      numericInput("nsamp",label = h5("Number in each sample"), step = 1, 
      value = 16, min = 2)
    ),

    mainPanel(

(我还需要弄清楚如何使用 calctype 将服务器定向到适当的 R 代码块。)

【问题讨论】:

标签: r shiny radio-button conditional


【解决方案1】:

这是一个适合您的工作示例:

library(shiny)


ui <- fluidPage(

  titlePanel("|Species Table"),
    sidebarLayout(

      sidebarPanel(
        radioButtons("calctype", "Choose calculation:",selected = "calcFPR", 
                     choices = c(
                       "prior (for given FPR and P val)" = "calcprior",
                       "P value (for given FPR and prior)" = "calcpval",    
                       "FPR (for given P value and prior)" = "calcFPR")),
        uiOutput("test1"),
        uiOutput("test2"),
        inputPanel(
          numericInput("nsamp",label = h5("Number in each sample"), step = 1, 
                       value = 16, min = 2)
        )),

    mainPanel(textOutput("text1"),
              textOutput("text2"))

))


server <- function(input, output) {

  test <- reactive({
    if(input$calctype == 'calcprior'){
      label1 <- paste("observed P value")
      label2 <- paste("false positive risk")
    }else if(input$calctype == 'calcpval'){
      label1 <- paste("false positive risk")
      label2 <-  paste("prior probability of real effect")
    }else{
      label1 <- paste("observed P value")
      label2 <- paste("prior probability of real effect")
    }
    list(label1 = label1, label2 = label2)
  })

  output$test1 <- renderUI({
    numericInput("test1", label = h5(test()$label1), value = 0.05, min = 0, max = 1)
  })

  output$test2 <- renderUI({
    numericInput("test2", label = h5(test()$label2), value = 0.05, min = 0, max = 1)
  })

  output$text1 <- renderText({
    input$test1
  })

  output$text2 <- renderText({
    input$test2
  })

}




shinyApp(ui = ui, server = server)

你不需要这么多conditionalPanels,我注意到在你的代码中唯一改变的是numericInput的标签,根据选择的计算类型。因此,在这种情况下,我在server 中使用了if...else... 语句来更改具有所选计算类型的两个`numericInputs(test1 和test2)的标签。

此外,我已打印出numericInput `s(text1 和 text2)的输出,以向您显示用户输入实际上已传递到服务器,并可进一步用于计算。

【讨论】:

  • 非常感谢。 R 社区真的很棒。今天早上我通过指向此页面的 twitter 链接获得了另一个解决方案 :-)
猜你喜欢
  • 1970-01-01
  • 2020-12-03
  • 2020-12-08
  • 1970-01-01
  • 2018-08-17
  • 1970-01-01
  • 1970-01-01
  • 2020-07-28
  • 1970-01-01
相关资源
最近更新 更多