gpt4 book ai didi

r - 在 R Shiny 中使用 renderText() 输出多行文本

转载 作者:行者123 更新时间:2023-12-01 19:42:35 24 4
gpt4 key购买 nike

我想使用一个 renderText() 命令输出多行文本。然而,这似乎不可能。例如,来自shiny tutorial我们在 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$text1output$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])})

大家有什么想法吗?

最佳答案

您可以使用renderUIhtmlOutput而不是renderTexttextOutput .

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功能。

关于r - 在 R Shiny 中使用 renderText() 输出多行文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23233497/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com