【问题标题】:Shiny extra white space in selectInput choice display labelselectInput 选择显示标签中闪亮的额外空白
【发布时间】:2017-03-23 15:04:48
【问题描述】:

我在闪亮中使用 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)
}

【问题讨论】:

  • 我使用pre() 找到了一些带有空格帮助的链接,但是因为在selectInput 函数调用中需要额外的空格,所以会更难。该函数接受值并在内部创建 html 代码。如果不深入研究源代码,我就没有办法。希望其他人能有更多的运气。
  • 有什么方法可以覆盖 selectInput 之外的标签?
  • 那将是一项艰巨的文本解析工作。这是可能的,但不切实际。

标签: html r shiny


【解决方案1】:

我通常将空格(ASCII 32)替换为“硬空格”(ASCII 160)。 在这种情况下,多个空格未被检测到。

由于 RStudio 不接受 ALT-160 作为“”,因此需要使用 intToUtf8(160) 动态注入符号 160。

注意:base::strrep() 不能正确处理符号 160,因此必须改用 stringi::stri_dup()

感谢 cmets 建议将生成的名称放入 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)

如果有意义,请告诉我。

【讨论】:

  • 有什么方法可以继续使用 selectInput?而不是添加 renderUI?
  • 是的,你是对的,没有必要使用renderUI()。请查看更新后的代码
  • 现在好点了吗?
  • 就我个人而言,我不得不使用intToUtf8(8194)(用于“en space”)来实现接近“等宽空间”的效果。见List of XML and HTML character entity references
猜你喜欢
  • 1970-01-01
  • 2016-03-01
  • 2021-09-23
  • 1970-01-01
  • 2021-08-26
  • 2014-04-30
  • 2018-06-14
  • 2013-03-07
  • 2017-04-04
相关资源
最近更新 更多