【问题标题】:Outputting multiple lines of text with renderText() in R shiny在 R 中使用 renderText() 输出多行文本
【发布时间】:2014-06-07 15:34:03
【问题描述】:

我想使用一个renderText() 命令输出多行文本。然而,这似乎是不可能的。例如,从shiny tutorial 我们截断了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$text1output$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


    【解决方案1】:

    您可以使用renderUIhtmlOutput 代替renderTexttextOutput

    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/>'))
    
      })
    }
    )
    )
    

    请注意,您需要使用&lt;br/&gt; 作为换行符。此外,您希望显示的文本需要进行 HTML 转义,因此请使用 HTML 函数。

    【讨论】:

    • 谢谢。我想没有html代码就没有办法做到这一点?
    • HTML() 包裹是离合器。乍一看,我担心这是一个 hack,并且是唯一可用的选项......但实际上这个答案中的大部分代码都来自问题的示例。在renderUI({}) 内部切换到HTML(),然后在ui.R 侧切换到htmlOutput() 非常简单。不错的答案!
    • @Alex 我猜你也可以使用 verbatimTextOutput() 而不是 textOutput(),但你可能不想要灰色阴影。
    • +1 不错的答案。你将如何格式化HTML() 中的文本?例如,HTML("&lt;b&gt;paste(str1, str2, sep = '&lt;br/&gt;')&lt;\b&gt;") 不起作用
    • 如果你有兴趣回答,我已经创建了这个thread
    【解决方案2】:

    根据Joe Cheng

    嗯,我不建议使用 renderUIhtmlOutput [以其他答案中解释的方式]。您正在获取基本上是文本的文本,并在不转义的情况下强制转换为 HTML(这意味着如果文本恰好包含一个包含特殊 HTML 字符的字符串,则可能会被错误地解析)。

    这个怎么样:

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

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

    【讨论】:

      【解决方案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], ".")
      })
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-10-29
        • 2017-01-12
        • 2018-02-28
        • 1970-01-01
        • 1970-01-01
        • 2019-12-22
        • 1970-01-01
        • 2018-05-13
        相关资源
        最近更新 更多