Home > OS >  Change UI text based on dropdown in R Shiny
Change UI text based on dropdown in R Shiny

Time:12-09

I am trying to create an App which changes the UI based on a dropdown langauge.

I would like to know how to change the "textinput" from "Favourite R package" to "Paquete R favorito" when Spanish is selected.

Also how to change the placeholder = "" to link up with a dropdown option.

App:

    languageChoices = c("English", "Spanish")

myPlaceholder_English = c("Tell me something interesting!")
myPlaceholder_Spanish = c("Dime algo interesante!")

shinyApp(
  ui = fluidPage(
    titlePanel("My App"),
    div(
      id = "form",
      
      textInput("language", "Language", ""),
      textInput("favourite_pkg", "Favourite R package"),
      checkboxInput("used_shiny", "I've built a Shiny app in R before", FALSE),
      sliderInput("r_num_years", "Number of years using R", 0, 25, 2, ticks = FALSE),
      textInput("desc", "Tell me something?", width = '100%', placeholder = ""),
      actionButton("submit", "Submit", class = "btn-primary")
    )
  ),
  server = function(input, output, session) {
  }
)

CodePudding user response:

Perhaps use updateTextInput

library(shiny)

shinyApp(
  ui = fluidPage(
    titlePanel("My App"),
    div(
      id = "form",
      
      selectInput("language", "Language", languageChoices),
      textInput("favourite_pkg", "Favourite R package"),
      checkboxInput("used_shiny", "I've built a Shiny app in R before", FALSE),
      sliderInput("r_num_years", "Number of years using R", 0, 25, 2, ticks = FALSE),
      textInput("desc", "Tell me something?", width = '100%', placeholder = ""),
      actionButton("submit", "Submit", class = "btn-primary")
    )
  ),
  server = function(input, output, session) {
    
    observe({
      
      x <- input$language
      val <- if(x == "Spanish") "Paquete R favorito" else "Favourite R package"
      updateTextInput(session, "favourite_pkg", value =val, label = val  )
      
    })
  }
)

-output enter image description here

  • Related