首页 > 解决方案 > Shiny 中的列联表,可选择输入组或总值并验证输入

问题描述

我正在 Shiny 中建立一个列联表。我正在使用numericInput和一些 CSS 来制作功能表,目前正在使用verbatimTextOutput来提供行和列总计。

这可行,但我想做的是能够在任何框中输入数字(包括总数)并让它更正其余部分。因此,例如,可以将文本输入到结果正面和总计列中,然后自动将其填充到结果负面列中。我还没有找到一个很好的方法来让它与验证一起工作。任何帮助,将不胜感激。

(ui.r)
fluidRow(strong("Contingency Table")),
        fluidRow(helptext("Input the ")),
        
        fluidRow(column(width = 3),
                 column(h5("Outcome Positive"), width = 3),
                 column(h5("Outcome Negative"), width = 3),
                 column(h5("Total"), width = 3)),
        
        fluidRow(column(h5("Intervention"), width = 3),
                 column(numericInput(inputId = "int_pos",
                                     label = NULL, 
                                     value = 117,
                                     min = 0),
                        width = 3),
                 column(numericInput(inputId = "int_neg",
                                     label = NULL, 
                                     value = 138,
                                     min = 0),
                        width = 3),
                 column(verbatimTextOutput("int_tot"),
                        width = 3)),
        
        fluidRow(column(h5("Control"), width = 3),
                 column(numericInput(inputId = "con_pos",
                                     label = NULL, 
                                     value = 109,
                                     min = 0),
                        width = 3),
                 column(numericInput(inputId = "con_neg",
                                     label = NULL, 
                                     value = 145,
                                     min = 0),
                        width = 3),
                 column(verbatimTextOutput("con_tot"),
                        width = 3)),
        
        fluidRow(column(h5("Total"), width = 3),
                 column(verbatimTextOutput("tot_pos"), width = 3),
                 column(verbatimTextOutput("tot_neg"), width = 3),
                 column(verbatimTextOutput("tot_tot"), width = 3))
      ),

列联表

server.R
  output$int_tot = renderText({
    input$int_neg + input$int_pos
  })
  
  output$con_tot = renderText({
    input$con_neg + input$con_pos
  })
  
  output$tot_pos = renderText({
    input$int_pos + input$con_pos
  })
  
  output$tot_neg= renderText({
    input$int_neg + input$con_neg
  })
  
  output$tot_tot = renderText({
    input$int_neg + input$con_neg + input$int_pos + input$con_pos
  })

标签: rshiny

解决方案


推荐阅读