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

标签 r shiny rendering

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

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

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()))
    }
  )

})

最佳答案

您可以在列表中使用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))
}))

关于r - 如何格式化 R Shiny 中的文本显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36368374/

相关文章:

css - R shiny tabset标题修改字体大小

javascript - "Long frame times are an indication of jank"

r - "zoom"/"scale"与 coord_polar()

r - 为什么当我将计算量减半时,我在时间上只得到了微小的改进?

r - 将R解释器和R作为共享库安装在同一棵树下

r - Shiny 的 Highcharts 的 y 轴没有自动调整大小

r - 使用 ggplot2 在 R 中创建 "combination tree"

r - 在给定的 react 中,是否有方法/选项来获取触发它的 react 元素?

android - GLSurfaceView.RENDERMODE_CONTINUOUSLY 导致闪烁

c++ - 行进立方体问题