2016-11-09 130 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

現在好了嗎? –