2016-11-09 84 views
3

我在光泽和我的选择下拉菜单中使用selectInput我想有些话之间的多个空格。然而,只包含空格将不会显示,在应用程序中最多只有一个空格。闪亮的额外空白

例如在代码示例下面我有“缸”和“我”之间的多个空格,但是如果你运行这个只有一个会显示 - 怎样才能解决这个问题?

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) 
} 
+0

我发现了一些使用'pre()'的空白帮助链接,但是因为在'selectInput'函数调用中需要额外的空格,所以它会更难。该函数接受值并在内部创建html代码。如果不深入源代码,我看不到一种方法。希望别人能有更多的运气。 –

+0

有没有办法覆盖selectInput之外的标签? – user7066213

+0

这将是一个艰难的文本解析工作。这是可能的,但不切实际。 –

回答

1

我通常用“hard space”(ASCII 160)替换空格(ASCII 32)。 在这种情况下,多个空间未被发现。作为一个“”一个需要使用intToUtf8(160)灌输符号160动态

作为RStudio不接受ALT-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) 

请让我知道它是否有意义。

+0

任何方式来继续使用selectInput?而不是添加renderUI? – user7066213

+0

是的,你说得对,没有必要使用'renderUI()'。请参阅更新代码 –

+0

现在好了吗? –