如何在 R Shiny 页面中“框”住简单文本

11
我将使用文档https://shiny.rstudio.com/tutorial/written-tutorial/lesson2/,具体是以下代码来将一个简单的段落添加到我的Shiny页面:
ui <- fluidPage(
  titlePanel("My Shiny App"),
  sidebarLayout(
    sidebarPanel(),
    mainPanel(
      p("p creates a paragraph of text."),
      p("A new p() command starts a new paragraph. Supply a style attribute to change the format of the entire paragraph.", style = "font-family: 'times'; font-si16pt"),
      strong("strong() makes bold text."),
      em("em() creates italicized (i.e, emphasized) text."),
      br(),
      code("code displays your text similar to computer code"),
      div("div creates segments of text with a similar style. This division of text is all blue because I passed the argument 'style = color:blue' to div", style = "color:blue"),
      br(),
      p("span does the same thing as div, but it works with",
        span("groups of words", style = "color:blue"),
        "that appear inside a paragraph.")
    )
  )
)

我的目标是将这些段落中的任何一个(比如最后一个)展示为一个盒子,就像我们在这里看到的一样: http://www.sthda.com/english/articles/40-regression-analysis/168-multiple-linear-regression-in-r/
在这个例子中,“library(tidyverse)”这个段落被包含在一个不同背景颜色的盒子中。 有谁知道我该如何实现这个效果? 由于我不太了解HTML,所以遇到了麻烦。 谢谢。

1
也许这里的答案(https://stackoverflow.com/questions/65259519/format-helptext-in-shinydashboardbox)可以帮助您。 - YBS
1个回答

18

重点不在于HTML,而是你应该寻找CSS。例如,你可以复制并粘贴你链接的网页中的CSS样式规则到你的shiny app中(这不是推荐的方式,但又快又脏),以此来修改code标签的外观:

library(shiny)

ui <- fluidPage(
    tags$head(
        tags$style(HTML("
            code {
                display:block;
                padding:9.5px;
                margin:0 0 10px;
                margin-top:10px;
                font-size:13px;
                line-height:20px;
                word-break:break-all;
                word-wrap:break-word;
                white-space:pre-wrap;
                background-color:#F5F5F5;
                border:1px solid rgba(0,0,0,0.15);
                border-radius:4px; 
                font-family:monospace;
            }"))),
    titlePanel("My Shiny App"),
    sidebarLayout(
        sidebarPanel(),
        mainPanel(
            p("p creates a paragraph of text."),
            p("A new p() command starts a new paragraph. Supply a style attribute to change the format of the entire paragraph.", style = "font-family: 'times'; font-si16pt"),
            strong("strong() makes bold text."),
            em("em() creates italicized (i.e, emphasized) text."),
            br(),
            code("code displays your text similar to computer code"),
            div("div creates segments of text with a similar style. This division of text is all blue because I passed the argument 'style = color:blue' to div", style = "color:blue"),
            br(),
            p("span does the same thing as div, but it works with",
              span("groups of words", style = "color:blue"),
              "that appear inside a paragraph.")
        )
    )
)

server <- function(input, output) {

}

shinyApp(ui = ui, server = server)

在此输入图片描述


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接