Angelo
Angelo

Reputation: 1765

Text output with a label or non-editable text input

I'm a Shiny newbie and I have probably a simple question, but I swear I spent half day reading solutions here and can't find anything close to what I need. Imaging you have a database with employees ID, name, last name, age. I need to have in my app a text input that allows the user to enter the ID and to see on the same row the name, last name and age. The problem I face is that textInput will have a label (say "ID"). All the three other fields, that I need to be on the same row, won't have a label. So what I would need is either a way to add a label to the three textOutput elements or to display them as textInput with a default value that has to change/behave like an output as soon as a user enters a new ID. But how? This is my sample code:

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)

Perhaps I have to use different widgets? Thank you

Upvotes: 0

Views: 2956

Answers (2)

Dwight
Dwight

Reputation: 124

How I create label for my UI is simply adding a h3 tag above each textoutput:

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"))
                                  )
                          )
                )
  )

Upvotes: 0

Tom
Tom

Reputation: 592

I updated the code to change the label using an textInput as suggested in the comment. Perhaps it helps to understand exactly what you are looking for.

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)

Upvotes: 1

Related Questions