例如,我有一个这样的字符串:
SomeName SomeValue
A much longer name AnotherValue
Even longer name than before NewValue
假设我在 R 变量中正确地包含了这段文本,例如
variable<-"SomeName SomeValue<br\>A much longer name AnotherValue<br\>Even longer name than before NewValue<br\>
在我的 ui.R 中:
...htmlOutput("TextTable")
在我的服务器中。R
output$TextTable<- renderUI({
HTML(variable)
})
但输出不是我想要的方式。除一个空格外,所有空格都被删除。所以“列”在我的输出中并不像它们应该的那样。我怎样才能避免这种情况?
最佳答案
如果我没记错的话,浏览器倾向于将 HTML 代码中的连续空格缩短为单个字符。不要将空格放在 r 变量中。将数据存储为矩阵,并使用 renderTable
.
##server.R
library(shiny)
shinyServer(function(input, output) {
variable = c("SomeName","SomeValue","A much longer name","AnotherValue","Even longer name than before", "NewValue")
variable = matrix(data = variable, ncol = 2)
output$TextTable<- renderTable({variable},include.rownames = FALSE,include.colnames = FALSE)
})
##ui.R
library(shiny)
shinyUI(fluidPage(titlePanel(""),
sidebarLayout(sidebarPanel(),
mainPanel(uiOutput("TextTable")
)
)
))
更新
另一方面,如果您的文本已经是那种格式,您可以使用 <pre>
来防止浏览器折叠空格。标签。在您的情况下,您可以使用以下代码:
output$TextTable<- renderUI(HTML(paste0("<pre>",variable,"</pre>")))
关于html - R Shiny : htmlOutput removes white spaces I need,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28878616/