【问题标题】:use renderUI to change the order of inputs on page使用 renderUI 更改页面上的输入顺序
【发布时间】:2021-11-15 10:30:32
【问题描述】:

我有一个数据输入表单,用户将使用该表单从纸质数据表中输入野生动物捕获数据。纸张有许多相同的字段(例如,年龄、体重、性别),但并非完全相同(例如,有些有chest_girth,有些没有)。表格也以不同的顺序设置,闪亮的表格必须与纸张上字段的确切顺序匹配。

我目前有一系列 uiOutput/renderUI 对,它们根据用户从下拉列表中选择的物种来显示输入。因为大多数物种都收集了相同的数据,并且这些数据都将进入同一个表,所以我想对所有物种使用一个输入(例如,input$Age 而不是input$BearAgeinput$ElkAge)。但是,正如我上面所说,所有表格的顺序都不同,所以请不要建议我对所有物种都使用相同的表格。

我知道我可以使用shinyjs::show()shinyjs::hide() 来更改输入是否可见,但是有没有办法根据用户输入更改现有对象的顺序,使用renderUI 或其他方式?

看起来像这样:

library(shiny)
ui <- fluidPage(
fluidRow(
  column(3, selectInput(inputId = 'species', label = 'Species', choices = c('Elk', 'Bear', 'Deer'))),
  column(3, numericInput(inputId = 'age', label = 'Age', value = NA)),
  column(3, numericInput(inputId = 'weight', label = 'Weight', value = NA))
)

server <- function(input, output, session) {
  if (input$species == 'bear') {
    *switch the order of age and weight*
   }

【问题讨论】:

    标签: html r shiny shinyjs renderui


    【解决方案1】:
    library(shiny)
    
    ui <- fluidPage(
      fluidRow(
        column(3, selectInput(inputId = 'species', label = 'Species', 
          choices = c('Elk', 'Bear', 'Deer'))),
        uiOutput("inputs_UI")
      )
    )
    
    server <- function(input, output, session) {
      output$inputs_UI <- renderUI({
        if (input$species == 'Bear') {
          tagList(
            column(3, numericInput(inputId = 'age', label = 'Age', value = NA)),
            column(3, numericInput(inputId = 'weight', label = 'Weight', value = NA))
          )
        } else {
          tagList(
            column(3, numericInput(inputId = 'weight', label = 'Weight', value = NA)),
            column(3, numericInput(inputId = 'age', label = 'Age', value = NA))
          )
        }
      })
       
    }   
    
    shinyApp(ui, server)
    

    【讨论】:

      猜你喜欢
      • 2012-02-26
      • 2015-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-22
      • 2023-04-09
      • 2023-04-09
      相关资源
      最近更新 更多