html - R Shiny : htmlOutput removes white spaces I need

标签 html r shiny

例如,我有一个这样的字符串:

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/

相关文章:

javascript - $ ('body' ).on ('blur' ) 在 IE8 中无法正常工作

javascript - 如何在 jQuery 中获取窗口选择中的单词数

php - 如何通过hyperlink-url提交参数并在php中检索它们?

html - 使用 Bootstrap 对齐元素的字段集未正确对齐

r - 从 github : Could not find build tools necessary to build data. 表安装 R 包时出错

r - 在列表中查找对象的位置编号

r - 将 R 中的矩阵转换为具有相应条目的上三角/下三角矩阵

r - 如何以 Shiny 的百分比显示 "numericInput"中的值?

r - 如何更改 Shinydashboard 中侧边栏的字体大小

r - 使用操作按钮的 Shiny 渲染图