如何格式化 R Shiny 中的文本显示?
How to format text display in R 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))
}))
我有一个列表对象,每个列表包含一个字符串,每个列表元素中的每个字符串代表一个段落。我正在尝试在闪亮的应用程序中显示结果,我可以在其中单独显示每个段落(中间有新行),但我还没有想出是否有办法做到这一点。
这是我目前的情况:
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))
}))