html - 如何在 Shiny 和 Shinydashboard 中自定义 HTML 文本和 verbatimTextOutput 之间的空间

标签 html css r shiny shinydashboard

这是我上一个问题 ( How to change the width and height of verbatimTextOutput in Shiny and Shinydashboard ) 的后续问题。我想创建一个看起来与 textInput 相同的 verbatimTextOutput。下面是一个例子。

现在除了标题和文本框之间的空格(如红线所示)外,几乎所有内容都相同。我使用带有 strong 函数的一行文本来模仿 textInput 中的标题。有没有办法增加此行和 erbatimTextOutput 之间的空间,使外观与 textInput` 相同?

enter image description here

代码

# Load the packages
library(shiny)
library(shinydashboard)

# User Interface
ui <- dashboardPage(
  header = dashboardHeader(title = ""),
  sidebar = dashboardSidebar(
    sidebarMenu(
      menuItem(
        text = "Example",
        tabName = "tab1"
      )
    )
  ),
  body = dashboardBody(
    tabItems(
      tabItem(
        tabName = "tab1",

        tags$head(
          tags$style(
            HTML(
              "
        .form-control {
            border-radius: 4px 4px 4px 4px;
        }

        #txt1_out {
        font-family:  'Source Sans Pro','Helvetica Neue',Helvetica,Arial,sans-serif;
        font-size: 14px;
        width: 300px; 
        max-width: 100%;
        padding: 6px 12px; 
        white-space: pre-wrap;
        }

        "
            )
          ),
          tags$script("
            Shiny.addCustomMessageHandler('selectText', function(message) {
              $('#txt2_out').prop('readonly', true);
            });
            ")
        ),
        column(
          width = 4,
          textInput(inputId = "txt1", label = "Text input no.1", value = "abcde12345"),
          strong("Text output no.1"),
          verbatimTextOutput(outputId = "txt1_out", placeholder = TRUE)
        )
      )
    )
  )
)

server <- function(input, output, session){
  output$txt1_out <- renderText({
    input$txt1
  })
}

# Run the app
shinyApp(ui, server)

最佳答案

您可以将 margin-top: 7px; 添加到:

#txt1_out {
font-family:  'Source Sans Pro','Helvetica Neue',Helvetica,Arial,sans-serif;
font-size: 14px;
width: 300px; 
max-width: 100%;
padding: 6px 12px; 
white-space: pre-wrap;
margin-top: 7px;
}

您可以使用这个值(6px?)。我首先使用 Chrome DevTools 检查了 Text input no.1,它具有:

label {
    display: inline-block;
    max-width: 100%;
    margin-bottom: 5px;
    font-weight: 700;
}

我尝试匹配 margin-bottom: 5px 但元素在所有方面都不相同,这就是我选择 7px 的原因。

关于html - 如何在 Shiny 和 Shinydashboard 中自定义 HTML 文本和 verbatimTextOutput 之间的空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58600719/

相关文章:

r - 删除在某些列中具有所有NA的行

r - 在共享服务器上运行 R 的包管理

r shiny - 获取单选按钮值作为变量

javascript - 从 ajax 检索时无法识别和显示 HTML 实体

css - 我的样式表中的图像无法加载

html - 如何居中对齐菜单中的元素?

html - 在 <head> 元素之外使用 &lt;style&gt; 标签是否正确?

php - 如何使用get方法滚动到页面的位置?

html - 使用正则表达式删除 <a href > 标签

javascript - 如何更新写在 Canvas 上的文本