1つのコマンドで複数行のテキストを出力したいのですrenderText()
が、これは不可能のようです。たとえば、光沢のあるチュートリアルコードは切り捨てられています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"))
))
基本的には 2 行を出力します。
You have selected example
You have chosen a range that goes from example range.
2行output$text1
をoutput$text2
1つのコードブロックにまとめることは可能でしょうか?これまでの私の努力は失敗しました。
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])})
何かアイデアはありますか?
ベストアンサー1
andの代わりにrenderUI
and を使うこともできます。htmlOutput
renderText
textOutput
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
関数を使用してください。