使用R shiny中的renderText()输出多行文本
我想用一个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])})
有人有主意吗?
您可以使用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
function。
Joe Cheng说 :
renderUI
,我不build议使用renderUI
和htmlOutput
[以在其他答案中解释的方式]。 您正在采取基本上是文本的文本,强制转换为HTML而不会转义(意思是说,如果文本恰好包含一个包含特殊HTML字符的string,则可能会被错误地parsing)。如何呢,而不是:
textOutput("foo"), tags$style(type="text/css", "#foo {white-space: pre-wrap;}")
(将#foo中的fooreplace为textOutput的ID)