带标签的文本输出或不可编辑的文本输入

时间:2019-05-01 12:28:25

标签: r shiny

我是一个有光泽的新手,我可能有一个简单的问题,但是我发誓我在这里花了半天时间阅读解决方案,找不到任何我需要的东西。 为您的数据库成像,其中包含员工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)

也许我必须使用其他小部件? 谢谢

2 个答案:

答案 0 :(得分:0)

我更新了代码,以使用注释中建议的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)

答案 1 :(得分:0)

我如何为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"))
                                  )
                          )
                )
  )
相关问题