【问题标题】:Why does R Shiny not recognize my line break command?为什么 R Shiny 无法识别我的换行命令?
【发布时间】:2021-11-12 10:18:26
【问题描述】:

我有一个简单的应用程序,我想在其中弹出一个文本,但由于文本很长,我想添加换行符。出于某种原因,即使我添加了
,R 也无法识别我的换行符,就像我在 example 中读到的那样。

任何帮助将不胜感激!

library(shiny)

long_text <- paste0(htmltools::HTML("I have a lot of text. <br><br>And I want it on different lines.<br><br> This should work, but R is being....<br><br>difficult."))


ui <- fluidPage(

    sidebarLayout(
        sidebarPanel(
            br(),
            actionButton(inputId = "text_info", 
                         label = "My R Sob Story", style = "color: #FFFFFF; background-color: #CA001B; border_color: #CA001B")
        ),

        mainPanel(
        )
    )
)

# Define server logic required to draw a histogram
server <- function(input, output) {

    observeEvent(input$text_info, {
        showModal(modalDialog(long_text, title = strong("Why R you doing this to me?")))
    })
}

# Run the application 
shinyApp(ui = ui, server = server)

这是现在的样子:

【问题讨论】:

    标签: html r text shiny line-breaks


    【解决方案1】:

    如果将文本更改为 HTML 后粘贴,它将再次成为字符。

    library(shiny)
    
    long_text <- htmltools::HTML("I have a lot of text. <br><br>And I want it on different lines.<br><br> This should work, but R is being....<br><br>difficult.")
    
    
    
    ui <- fluidPage(
      
      sidebarLayout(
        sidebarPanel(
          br(),
          actionButton(inputId = "text_info", 
                       label = "My R Sob Story", style = "color: #FFFFFF; background-color: #CA001B; border_color: #CA001B")
        ),
        
        mainPanel(
        )
      )
    )
    
    # Define server logic required to draw a histogram
    server <- function(input, output) {
      
      observeEvent(input$text_info, {
        showModal(modalDialog(long_text, title = strong("Why R you doing this to me?")))
      })
    }
    
    # Run the application 
    shinyApp(ui = ui, server = server)
    

    【讨论】:

    • !很高兴知道这很有效!如果您不介意一个后续问题,如果 R 将其改回字符,那么为什么 \n\n 不起作用?所以不会让我再接受 5 分钟的答案,但我会接受。谢谢!
    • 是的,好问题..我也试过了,但我不确定为什么会这样。我想用\n 在HTML 上显示链接中断不是straightforward
    猜你喜欢
    • 1970-01-01
    • 2020-11-07
    • 1970-01-01
    • 2021-05-29
    • 2020-08-15
    • 1970-01-01
    • 1970-01-01
    • 2012-01-27
    • 2020-12-09
    相关资源
    最近更新 更多