使用R中的renderText()输出多行文本 [英] Outputting multiple lines of text with renderText() in R shiny

查看:8
本文介绍了使用R中的renderText()输出多行文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望使用一个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, "
", "You have chosen a range that goes from", input$range[1], "to", input$range[2])})

有人有什么想法吗?

推荐答案

可以使用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/>'))

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

这篇关于使用R中的renderText()输出多行文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆