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

RShiny仪表板中timevis时间线自定义颜色失效求助

Fix: timevis Style Not Working in Shiny Dashboard

I’ve run into this exact issue before—timevis works perfectly with custom styles in a standalone script, but when ported to Shiny, all items default back to blue. Let’s break down the most likely fixes:

1. Ensure Your Data is Reactive (Critical!)

In your test environment, you’re probably using static data, but Shiny relies on reactive contexts to pass data correctly to the UI. If timevisData isn’t defined as a reactive object in your server, the style column might not be passed to the timevis output.

Fix: Wrap Your Data in reactive()

Update your server.R to make timevisData reactive, then call it with () in renderTimevis:

server <- function(input, output) {
  # Define reactive data source
  timevisData <- reactive({
    data.frame(
      content = tijdlijntabel$machine_nr,
      start = tijdlijntabel$onderhoud_datum,
      group = tijdlijntabel$onderhoud_type,
      style = tijdlijntabel$style,
      stringsAsFactors = FALSE  # Ensure style stays as character (for R <4.0)
    )
  })

  output$timeline_aalst <- renderTimevis({
    groups <- data.frame(id = c("Klep", "Reinigen", "Zeeppomp"), 
                         content = c("Klep", "Reinigen", "Zeeppomp"))
    timevis(data = timevisData(),  # Call reactive data with ()
            groups = groups, 
            showZoom = TRUE, 
            options = list(editable = TRUE)) %>% 
      setWindow(Sys.Date() %m-% months(1), Sys.Date() %m+% months(1))
  })
}

2. Check for Factor Conversion of the Style Column

Older R versions (pre-4.0) automatically convert character columns to factors, which timevis can’t interpret as valid CSS. Even in newer versions, sometimes data import functions (like read.csv) might force factor conversion accidentally.

Fix: Force Style Column to Be Character

Add as.character() to your style column when creating timevisData:

style = as.character(tijdlijntabel$style)

Or use stringsAsFactors = FALSE in your data.frame() call (as shown above).

3. Override Shiny’s Default CSS (If Styles Are Being Overwritten)

Sometimes Shiny’s built-in CSS can override your inline styles. A more reliable approach is to use className instead of style, then define custom CSS classes in your UI.

Step 1: Update Data to Use Class Names

Replace the style column with className in timevisData:

timevisData <- reactive({
  data.frame(
    content = tijdlijntabel$machine_nr,
    start = tijdlijntabel$onderhoud_datum,
    group = tijdlijntabel$onderhoud_type,
    # Map your condition to class names (adjust the logic to match your data)
    className = ifelse(tijdlijntabel$status == "uitgevoerd", "green-check", "red-scheduled"),
    stringsAsFactors = FALSE
  )
})

Step 2: Add Custom CSS to UI

Update your ui.R to include the CSS styles for these classes:

ui <- fluidPage(
  tags$head(
    tags$style(HTML("
      /* Custom timevis styles */
      .timevis-item.green-check {
        border-color: green !important;
        color: white !important;
        background-color: green !important;
      }
      .timevis-item.red-scheduled {
        border-color: red !important;
        color: white !important;
        background-color: red !important;
      }
    "))
  ),
  h3("Overzicht uitgevoerde en voorgestelde onderhoudsbeurten"),
  timevisOutput("timeline_aalst")
)

The !important flag ensures your styles take precedence over Shiny’s defaults.

Start with fix #1 first—it’s the most common culprit when moving from standalone R scripts to Shiny. If that doesn’t work, check for factor conversion, then try the CSS override method.

内容的提问来源于stack exchange,提问作者Eline Haldermans

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 08:05:12