【问题标题】:R Shiny: htmlOutput removes white spaces I needR Shiny:htmlOutput 删除我需要的空格
【发布时间】:2015-03-05 13:08:56
【问题描述】:

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

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 函数有什么作用?可能是在剥离空格?

标签: html r shiny


【解决方案1】:

如果我没记错的话,浏览器倾向于将 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")
    )
  )
))

更新

另一方面,如果您的文本已经采用该格式,您可以使用&lt;pre&gt; 标记防止浏览器折叠空格。在您的情况下,您可以使用以下代码:

output$TextTable<- renderUI(HTML(paste0("<pre>",variable,"</pre>"))) 

【讨论】:

  • 不幸的是我不能这样做。我的文本文件用于以这种格式显示数据,并作为文本文件导入到 word 等中。所以我需要处理这些空格,我需要显示它们
  • 你说得对,我并没有真正回答你的实际问题。希望我刚刚对答案所做的更改更适合您的情况。
  • 太棒了!非常感谢保罗!
猜你喜欢
  • 1970-01-01
  • 2018-12-27
  • 1970-01-01
  • 2021-05-02
  • 1970-01-01
  • 2021-08-03
  • 2023-03-22
  • 2018-08-12
  • 2020-02-09
相关资源
最近更新 更多