Program Tip

R에서 renderText ()로 여러 줄의 텍스트 출력 Shiny

programtip 2020. 12. 8. 19:54
반응형

R에서 renderText ()로 여러 줄의 텍스트 출력 Shiny


하나의 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"))
))

본질적으로 두 줄을 인쇄합니다.

You have selected example
You have chosen a range that goes from example range.

이 두 줄을 결합하는 것이 가능 output$text1하고 output$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함수를 사용하십시오 .


Joe Cheng 에 따르면 :

Uhhh I don't recommend using renderUI and htmlOutput [in the way that is explained in the other answer]. You are taking text that is fundamentally text, and coercing to HTML without escaping (meaning if the text just happens to include a string that contains special HTML characters, it could be parsed incorrectly).

How about this instead:

textOutput("foo"),
tags$style(type="text/css", "#foo {white-space: pre-wrap;}")

(Replace the foo in #foo with the ID of your textOutput)


If you mean that you don't care for the line break:

output$text = renderText({
  paste("You have selected ", input$var, ". You have chosen a range that goes 
  from", input$range[1], "to", input$range[2], ".")
})

참고URL : https://stackoverflow.com/questions/23233497/outputting-multiple-lines-of-text-with-rendertext-in-r-shiny

반응형