Reputation: 87
I am using selectInput in shiny and within the dropdown menu of my choices I want multiple white spaces between some words. However just including white spaces will not be displayed, in the app there will be at most one white space.
For instance in the code example below I have multiple spaces between "Cylinder" and "I", however if you run this only one will be displayed - how can I solve this?
ui <- fluidPage(
selectInput("variable", "Variable:",
c("Cylinder I want multiple spaces here" = "cyl",
"Transmission" = "am",
"Gears" = "gear")),
tableOutput("data")
)
server <- function(input, output) {
output$data <- renderTable({
mtcars[, c("mpg", input$variable), drop = FALSE]
}, rownames = TRUE)
}
shinyApp(ui, server)
}
Upvotes: 6
Views: 2031
Reputation: 540
I usually replace space (ASCII 32) with "hard space" (ASCII 160). In this case multiple spaces come undetected.
As RStudio does not accept ALT-160 as a " " one needs to use intToUtf8(160)
to infuse symbol 160 dynamically.
Note: base::strrep()
does not process symbol 160 properly so one has to use stringi::stri_dup()
instead.
Thank you for comments suggesting to put generated name inside selectInput()
. The resulting solution is as follows:
library(shiny)
library(shinydashboard)
library(stringi)
# Praparations (could be put into global.R) ------------
choices <- c(
"TO BE OVERRIDEN" = "cyl",
"Transmission" = "am",
"Gears" = "gear")
# Replace name place holder with the actual one
names(choices)[1] <- paste0(
"Cylinder",
stri_dup(intToUtf8(160), 6), # Replace 6 with the desired number
"I want multiple spaces here")
# Definition of UI -----------
ui <- fluidPage(
selectInput("variable", "Variable:", choices),
tableOutput("data")
)
# Definition of server -----------
server <- function(input, output) {
# Main table
output$data <- renderTable({
mtcars[, c("mpg", input$variable), drop = FALSE]
}, rownames = TRUE)
}
# Run app -------
shinyApp(ui, server)
Please let me know if it makes sense.
Upvotes: 6