在R闪亮中使用renderText()输出多行文本

问题描述 投票:0回答:4

我想使用一个

renderText()
命令输出多行文本。然而,这似乎不可能。例如,在shiny教程中,我们在
server.R
中截断了代码:

shinyServer(
  function(input, output) {
    output$text1 <- renderText({paste("You have selected", input$var)
    output$text2 <- renderText({paste("You have chosen a range that goes from",
      input$range[1], "to", input$range[2])})
  }
)

以及

ui.R
中的代码:

shinyUI(pageWithSidebar(

  mainPanel(textOutput("text1"),
            textOutput("text2"))
))

本质上打印两行:

You have selected example
You have chosen a range that goes from example range.

是否可以将两行

output$text1
output$text2
组合成一个代码块?到目前为止我的努力都失败了,例如

output$text = renderText({paste("You have selected ", input$var, "\n", "You have chosen a range that goes from", input$range[1], "to", input$range[2])})

大家有什么想法吗?

r shiny
4个回答
125
投票

您可以使用

renderUI
htmlOutput
代替
renderText
textOutput

require(shiny)
runApp(list(ui = pageWithSidebar(
  headerPanel("censusVis"),
  sidebarPanel(
    helpText("Create demographic maps with 
      information from the 2010 US Census."),
    selectInput("var", 
                label = "Choose a variable to display",
                choices = c("Percent White", "Percent Black",
                            "Percent Hispanic", "Percent Asian"),
                selected = "Percent White"),
    sliderInput("range", 
                label = "Range of interest:",
                min = 0, max = 100, value = c(0, 100))
  ),
  mainPanel(textOutput("text1"),
            textOutput("text2"),
            htmlOutput("text")
  )
),
server = function(input, output) {
  output$text1 <- renderText({paste("You have selected", input$var)})
  output$text2 <- renderText({paste("You have chosen a range that goes from",
                                    input$range[1], "to", input$range[2])})
  output$text <- renderUI({
    str1 <- paste("You have selected", input$var)
    str2 <- paste("You have chosen a range that goes from",
                  input$range[1], "to", input$range[2])
    HTML(paste(str1, str2, sep = '<br/>'))

  })
}
)
)

请注意,您需要使用

<br/>
作为换行符。此外,您希望显示的文本需要进行 HTML 转义,因此请使用
HTML
函数。


12
投票

根据Joe Cheng

呃,我不建议使用

renderUI
htmlOutput
[按照其他答案中解释的方式]。您正在获取本质上是文本的文本,并在不转义的情况下强制转换为 HTML(这意味着如果文本恰好包含包含特殊 HTML 字符的字符串,则可能会错误地解析)。

这个怎么样:

textOutput("foo"),
tags$style(type="text/css", "#foo {white-space: pre-wrap;}")

(将#foo中的foo替换为你的textOutput的ID)


0
投票

第一次询问时不知道此选项是否可用,但我发现使用 renderUI 中的

shiny::markdown
函数是最简单的选项。特别是因为在我的例子中,我正在渲染从带有换行符的文件中读取的文本 。当然,您应该注意对 Markdown 的特殊字符进行适当处理。

复制上面的示例给出:

require(shiny)
runApp(list(ui = pageWithSidebar(
  headerPanel("censusVis"),
  sidebarPanel(
    helpText("Create demographic maps with 
      information from the 2010 US Census."),
    selectInput("var", 
                label = "Choose a variable to display",
                choices = c("Percent White", "Percent Black",
                            "Percent Hispanic", "Percent Asian"),
                selected = "Percent White"),
    sliderInput("range", 
                label = "Range of interest:",
                min = 0, max = 100, value = c(0, 100))
  ),
  mainPanel(textOutput("text1"),
            textOutput("text2"),
            htmlOutput("text")
  )
),
server = function(input, output) {
  output$text1 <- renderText({paste("You have selected", input$var)})
  output$text2 <- renderText({paste("You have chosen a range that goes from",
                                    input$range[1], "to", input$range[2])})
  output$text <- renderUI({
    str1 <- paste("You have selected", input$var)
    str2 <- paste("You have chosen a range that goes from",
                  input$range[1], "to", input$range[2])
    markdown(paste(str1, str2, sep = '\n\n'))
    
  })
}
)
)

-3
投票

如果您的意思是您不关心换行符:

output$text = renderText({
  paste("You have selected ", input$var, ". You have chosen a range that goes 
  from", input$range[1], "to", input$range[2], ".")
})
© www.soinside.com 2019 - 2024. All rights reserved.