【问题标题】:How to reduce height of input fields in Shiny?如何减少 Shiny 中输入字段的高度?
【发布时间】:2020-02-13 03:09:52
【问题描述】:

我的 shinyApp 中有多个输入字段(fileInput、numericInput、textInput),我想自定义它们的高度以及字符大小。 我试过div(),但我只能改变两个字段之间的差距。在这种情况下,设置div(style="height: 60px;",numericInput("rat","RATIO", value = 0,step=0.01 , width = '40%')) 只会减少数字输入字段和滑块之间的距离。

这是一个示例代码:

sidebar <- dashboardSidebar(
  sidebarMenu(
    div(style="height: 70px;",fileInput('uploadfile',"Select result file(s)", multiple=TRUE,accep=".txt")),
    div(style="height: 60px;",numericInput("rat","RATIO", value = 0,step=0.01 , width = '40%')),
    div(style="height: 60px;",sliderInput("ratio",NULL, min= 0, max= 1, value = 0)),
    textInput("mytext","Enter name",value='', width = '50%')
  )
)

ui<-dashboardPage(
  dashboardHeader(title = "Analysis"),
  sidebar,
  body <- dashboardBody()
)

server<-shinyServer(function(input, output, session){})
shinyApp(ui = ui, server = server)

我从来没有做过任何 html,所以我不确定我应该寻找什么。

【问题讨论】:

    标签: css r shiny


    【解决方案1】:

    使用 CSS 有几种方法可以做到这一点。

    您可以更改所有具有相同 CSS 类的输入。然后所有相同类型的输入都将以相同的方式设置样式。或者您使用您知道 ui 元素的 id 的知识。对我来说,后者听起来对你来说更有趣,因为你似乎想为每个输入做特定的样式。

    在 shiny 中,您可以使用 tags$style() 命令覆盖现有的 CSS。您可以使用#id{property: value} 格式。因此对于带有 id 上传文件的输入文件,您可以使用:#uploadfile{height: 70px}。 (请注意,如果您对改编课程感兴趣,可以使用 .className{property: value}

    可重现的例子:

    sidebar <- dashboardSidebar(
      sidebarMenu(
        tags$head(
          tags$style(
            HTML('
            #uploadfile{height: 70px}
            #rat{height: 60px}
            #ratio{height: 60px}
            #mytext{width: 50px}
        ')
          )
        ),
        fileInput('uploadfile',"Select result file(s)", multiple=TRUE,accep=".txt"),
        numericInput("rat","RATIO", value = 0,step=0.01 , width = '40%'),
        sliderInput("ratio",NULL, min= 0, max= 1, value = 0),
        textInput("mytext","Enter name",value='', width = '50%')
      )
    )
    
    ui<-dashboardPage(
      dashboardHeader(title = "Analysis"),
      sidebar,
      body <- dashboardBody()
    )
    
    server<-shinyServer(function(input, output, session){})
    shinyApp(ui = ui, server = server)
    

    【讨论】:

    • 感谢@BigDataScientist 的解释。你知道我可以同时减少/增加输入文本的大小,并相应地调整两个字段之间的间隙吗?
    • 啊,那么我将输入您用于输入之间空间的 div 方法。这是个好主意。
    • 谢谢,确实,使用tags$style()heightfont-size)和div() 成功了。
    • 啊,我不知道两者之间的差距也是有意的。但无论如何,你自己想通了。我很高兴它奏效了。如果它对你来说是完整的,我将不胜感激。
    • 当然,没问题 :) 感谢您的宝贵时间!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-23
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    • 2011-02-08
    • 1970-01-01
    • 2020-08-06
    相关资源
    最近更新 更多