You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R Shiny中如何为日期范围输入框与数值框设置圆角边框

R Shiny中如何为日期范围输入框与数值框设置圆角边框

嘿,我帮你梳理下这个问题!你已经成功给selectizeInput加上了圆角,但dateRangeInput和valueBox没生效,主要是因为这两个组件的CSS选择器没找对,还有样式优先级和冲突的问题,咱们一步步来调整:

1. 搞定日期范围输入框(dateRangeInput)

dateRangeInput的结构里,实际的输入框是嵌套在.input-daterange容器下的.form-control元素,而且默认左右两个输入框会共享中间的分隔符,所以咱们需要给内部的输入框设置圆角,还要处理分隔符的样式:

tags$style(HTML("
  /* 给日期范围的每个输入框设置全圆角 */
  .input-daterange .form-control {
    border-radius: 50px !important;
    /* 可选:调整内边距让文字不贴太近圆角 */
    padding-left: 15px;
  }
  /* 去掉中间的分隔边框,避免视觉冲突 */
  .input-daterange .input-group-addon {
    border: none;
    background-color: transparent;
  }
"))

2. 修复数值框(valueBox)的圆角

你之前的代码里给valueBox加了多个冲突的样式(比如设置width: 0px会直接把盒子弄没,还有重复的style属性),咱们统一调整.small-box的样式,确保优先级足够:

tags$style(HTML("
  /* 给valueBox的外层容器设置圆角,去掉默认边框 */
  .small-box {
    border-radius: 50px !important;
    border: none !important;
    /* 可选:调整高度让数值显示更合理 */
    height: 80px;
  }
  /* 调整valueBox内部文字的垂直居中 */
  .small-box .inner {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
  }
"))

修改后的完整代码

我把你的代码整合了修改后的样式,去掉了冲突的设置:

library(shiny)
library(tidyverse)
library(scales)
library(ggpubr)
library(corrr)
library(shinydashboard)
library(DT)

dataset<-iris%>%
  mutate(week=seq(from=as.Date("2017-03-12"), to=as.Date("2020-01-22"), by = "weeks"))

ui <- fluidPage(
  tags$style('.container-fluid {
    background-color: Lavender;
  }'),
  tags$style(".small-box.bg-yellow { background-color: #FFFF00 !important; color: #000000 !important;
              border-radius:50px !important;}"),
  
  # 统一设置圆角样式
  tags$style(HTML("
    /* 日期范围输入框样式 */
    .input-daterange .form-control {
      border-radius: 50px !important;
      padding-left: 15px;
    }
    .input-daterange .input-group-addon {
      border: none;
      background-color: transparent;
    }
    
    /* ValueBox样式 */
    .small-box {
      border-radius: 50px !important;
      border: none !important;
      height: 80px;
    }
    .small-box .inner {
      display: flex;
      align-items: center;
      justify-content: center;
      height: 100%;
    }
    
    /* 可选:如果需要selectizeInput的圆角 */
    .selectize-input {
      border-radius: 50px !important;
    }
  ")),
  
  fluidRow(column(1,align='right',offset=7,
                  div(valueBoxOutput("vbox"),style="color:green;"),
                  style="background-color:#6724C6;",
                  style = "border-style: solid"),
           column(4,align='right',
                  dateRangeInput("week", "Week",width="400",
                                 end="2020-01-22", start="2017-03-12"))),
  
  fluidRow(
    column(4,
           selectInput("variable1", "Variable 1:",selected="Petal.Length",
                       choices=names(dataset[1:4]))),
    column(4,
           selectInput("variable2", "Variable 2:",selected="Sepal.Length",
                       choices=names(dataset[1:4]))),
    column(4,
           selectInput("variable3", "Variable 3:",selected="Species",
                       choices=names(dataset[5])))),
  
  mainPanel(width=12,
            plotOutput("distPlot"),
            dataTableOutput("txt"),
            verbatimTextOutput("write"),
  ),
  
  fluidRow(column(12,textInput("caption","Caption","Data Summary")))
)

server <- function(input, output) {
  
  output$vbox <- renderValueBox({
    valueBox(
      value = tags$p( round(cor(q()[,input$variable1],
                                q()[,input$variable2]),2), style = "font-size: 100%;"),
      subtitle = NULL
    )
  })
  
  q<- reactive({
    dataset%>%
      filter(week>= min(input$week)&
               week<=max(input$week))
  })
  
  output$distPlot <- renderPlot({
    print(ggplot(q(),aes_string(input$variable1, input$variable2))+geom_smooth())
  })
  
  output$txt<-renderDataTable({
    datatable(
      q()[,c(input$variable1,input$variable2)],
      options = list(
        scrollX = TRUE,
        scrollY = "250px"
      )
    )
  })
  
  output$write <- renderText({ input$caption })
}

shinyApp(ui = ui, server = server)

关键修改点说明

  • 把分散的tags$style合并成一个HTML块,避免样式冲突
  • 针对dateRangeInput的内部.form-control设置圆角,处理了中间分隔符的样式
  • 移除了valueBox代码里错误的width: 0px设置,调整了高度和内部文字居中
  • 用!important确保自定义样式覆盖Shiny的默认bootstrap样式

备注:内容来源于stack exchange,提问作者awz1

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.22 15:03:15