我有一个列表对象,每个列表都包含一个字符串,每个列表元素中的每个字符串代表一个段落。我试图在一个 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/