如何格式化R Shiny中的文本显示?

时间:2016-04-02 02:03:30

标签: r shiny rendering

我有一个列表对象,每个列表包含一个字符串,每个列表元素中的每个字符串代表一个段落。我试图在一个闪亮的应用程序中显示结果,我可以单独显示每个段落(中间有新行),但我还没弄清楚是否有办法做到这一点。

这是我到目前为止所拥有的:

shinyUI(
  fluidPage(

  # Application title.
  titlePanel("M&A Clearing House"),

  sidebarLayout(
    sidebarPanel(
      # Copy the line below to make a text input box
      textInput("company", label = h3("Company Name"), value = "Enter text..."),


      selectInput("year", "Choosing Year Range", 
                  choices = c("2014", "2015", "2016"),
                  selected="2015", multiple=T),

      actionButton("submit","Submit")
    ),

    mainPanel(
      tabsetPanel(
        tabPanel("All results", textOutput("allresult"))
      )
    )
  )
))


shinyServer(function(input, output, session) {

 # ....assuming the result generated would look like this:...
 # exampletext <- list()
 # exampletext[[1]] <- "this is a paragraph"
 # exampletext[[2]] <- "this is another paragraph"
 # exampletext ....assuming the result generated would look like this:...

  output$allresult <- renderPrint({
    return(unlist(exampletext()))
    }
  )

})

1 个答案:

答案 0 :(得分:4)

您可以使用列表中的lapply生成要输出的p标记。在UI代码上,将textOutput替换为:

htmlOutput("allresult")

在服务器代码上,请改用:

output$allresult <- renderUI(lapply(exampletext, tags$p))

这是一个完整的工作示例:

library(shiny)
shinyApp(shinyUI(
  fluidPage(
    sidebarLayout(
      sidebarPanel(),
      mainPanel(
        tabsetPanel(
          tabPanel("All results", 
                   htmlOutput("allresult"))
        )
      )
    )
  )
),
shinyServer(function(input, output) {
  exampletext <- rep(as.list("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."), 5)
  output$allresult <- renderUI(lapply(exampletext, tags$p))
}))