【问题标题】:Text output with a label or non-editable text input带有标签的文本输出或不可编辑的文本输入
【发布时间】:2020-10-10 12:20:10
【问题描述】:

我是一个闪亮的新手,我可能有一个简单的问题,但我发誓我在这里花了半天时间阅读解决方案,找不到任何接近我需要的东西。 想象您有一个包含员工 ID、姓名、姓氏、年龄的数据库。 我需要在我的应用程序中有一个文本输入,允许用户输入 ID 并在同一行上查看姓名、姓氏和年龄。 我面临的问题是 textInput 将有一个标签(比如“ID”)。 我需要在同一行的所有其他三个字段都没有标签。 因此,我需要一种向三个 textOutput 元素添加标签或将它们显示为 textInput 的方法,该默认值必须在用户输入新 ID 后立即更改/表现得像输出一样。但是怎么做? 这是我的示例代码:

library(shiny) 
u <- fluidPage(   
  titlePanel("Simple Selectable Reactive Function"),   
  sidebarLayout(
                sidebarPanel(),
                mainPanel(
                          h2("Results"),
                          fluidRow(column(2,
                                          textInput("input_ID", label = "Cusip 1",
                                                    value = "123")),
                                  column(2,
                                         textOutput(outputId = "output_name")),
                                  column(2,
                                         textOutput(outputId = "output_lastname")),
                                  column(2,
                                         textOutput(outputId = "output_age"))
                                  )
                          )
                )
  )

s <- function(input,output){
        output$output_name <- renderText(
          {             # here is where I will check in a database
            paste("Sample Name")   
          })    
        output$output_lastname <- renderText(
          {             # here is where I will check in a database
            paste("Sample Last Name")   
          })
        output$output_age <- renderText(
          {             # here is where I will check in a database
            paste("Sample Age")   
          })
  } 
shinyApp(ui=u,server=s)

也许我必须使用不同的小部件? 谢谢

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    按照评论中的建议,我更新了代码以使用 textInput 更改标签。也许它有助于准确地了解您在寻找什么。

    library(dplyr)
    library(shiny)
    library(shinyjs)
    u <- fluidPage(
      titlePanel("Simple Selectable Reactive Function"),
      sidebarLayout(
        sidebarPanel(),
        mainPanel(
          h2("Results"),
          fluidRow(
            column(2, textInput("input_ID", label = "Cusip 1",value = "123")),
            column(2, textInput("output_name", label = "Firstname") %>% disabled()),
            column(2, textInput("output_lastname", label = "Lastname") %>% disabled()),
            column(2, textInput("output_age", label = "Age") %>% disabled())))))
    
    s <- function(input,output, session){
    
      observe({
        id <- input$input_ID
        set.seed(id)
        df <- list(firstname = sample(LETTERS, 1), lastname = sample(LETTERS, 1), age = sample(1:100, 1))
        updateTextInput(session, inputId = "output_name", label = df[["firstname"]])
        updateTextInput(session, inputId = "output_lastname", label = df[["lastname"]])
        updateTextInput(session, inputId = "output_age", label = df[["age"]])
      })
    }
    shinyApp(ui=u,server=s)
    

    【讨论】:

    • 嗨,汤姆,感谢您的回答,但这并不能解决我的问题。如果你运行你的应用程序,每个输出框的标签在哪里?你怎么知道名字、姓氏和年龄?这就是我想要达到的目标。为输出字段设置标签。
    • 在这种情况下,我没有正确理解您的问题。考虑到这一点,我不确定您希望应用程序的行为如何。如果有帮助,您可以使用textInput 作为“输出”并使用updateTextInput 更新标签。您也可以使用shinyjs::disable 禁用它。
    • 这确实回答了我的问题!因此,请尝试在同一行上可视化 4 个字段。一个是输入,三个是输出。所有 4 个字段都需要一个标签(ID、姓名、姓氏、年龄)。这些标签都在网页上以相同的高度对齐。然后,都有一个值,只是第一个字段是输入,其他三个是输出。同样,这些值必须在相同的高度对齐。如果我们使用 textOutput,我们将没有它们的标签。您的解决方案有帮助。谢谢
    • 很高兴听到这有帮助。
    【解决方案2】:

    我如何为我的 UI 创建标签只是在每个文本输出上方添加一个 h3 标签:

    library(shiny) 
    u <- fluidPage(   
      titlePanel("Simple Selectable Reactive Function"),   
      sidebarLayout(
                    sidebarPanel(),
                    mainPanel(
                              h2("Results"),
                              fluidRow(column(2,
                                              textInput("input_ID", label = "Cusip 1",
                                                        value = "123")),
                                      column(2,
                                             h3("First Name: "),
                                             textOutput(outputId = "output_name")),
                                      column(2,
                                             h3("Last Name: "),
                                             textOutput(outputId = "output_lastname")),
                                      column(2,
                                             h3("Age: ),
                                             textOutput(outputId = "output_age"))
                                      )
                              )
                    )
      )
    

    【讨论】:

      猜你喜欢
      • 2015-03-28
      • 2020-03-14
      • 1970-01-01
      • 1970-01-01
      • 2011-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多