我正在使用闪亮的 selectInput ,并且在我的选择的下拉菜单中我想要在一些单词之间有多个空格。但只包含空格是不会显示的,应用程序中最多有一个空格。
例如,在下面的代码示例中,“Cylinder”和“I”之间有多个空格,但是如果运行此命令,只会显示一个空格 - 我该如何解决这个问题?
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)
}
Run Code Online (Sandbox Code Playgroud)
我通常用“硬空格”(ASCII 160)替换空格(ASCII 32)。在这种情况下,多个空格未被检测到。
由于 RStudio 不接受 ALT-160 作为“”,因此需要使用intToUtf8(160)动态注入符号 160。
注意:base::strrep()无法正确处理符号 160,因此必须使用它stringi::stri_dup()来代替。
感谢您建议将生成的名称放入其中的评论selectInput()。得到的解决方案如下:
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)
Run Code Online (Sandbox Code Playgroud)
请告诉我是否有意义。