R Shiny CondtionalPanel不使用CSS样式表

时间:2017-12-21 15:17:46

标签: css r shiny rpivottable

我正在使用R Shiny来构建Web应用程序。

我正在使用conditionPanel来(有时)显示一个数据透视表,具体取决于对象的类型df

如下所示,如果数据透视表显示在条件面板中,则只会忽略css,并且数据透视表将以默认样式显示。但是如果我包含第二个pivottable,而不是在conditionalpanel中呈现,则两个pivottables都是custom.css中描述的样式。

如果没有第二个样式表,我怎样才能确保样式表用于第一个pivottable?

# Server.R
server <- shinyServer(function(input, output,session){
  df <- data.frame(col1 = c('a','b','c'),
                   col2 = c(1,2,3))

  ## Output PivotTable
  output$pivotTable <- rpivotTable::renderRpivotTable({
    rpivotTable(data = df,
                aggregatorName = 'Sum',
                rendererName = 'Table')
  })

  ## Output PivotTable2
  output$pivotTable2 <- rpivotTable::renderRpivotTable({
    rpivotTable(data = df,
                aggregatorName = 'Sum',
                rendererName = 'Table')
  })

  condition <- ifelse(is.data.frame(df), 'true', 'false')

  ## Output PivotTable
  output$panelTable <- renderUI({
    conditionalPanel(
      condition,
      rpivotTableOutput("pivotTable")
    )
  })
})



# UI.R:
ui <- dashboardPage(
  title = "",

  ## Header content + dropdownMenu
  dashboardHeader(
    title = tags$b(""),
    titleWidth = 250
  ),

  ## Sidebar content
  dashboardSidebar(
    width = 250,
    sidebarMenu(
      id = "tabs",
      menuItem("tab1", tabName = "tab", icon = icon("table"))
    )
  ),

  ## Body content
  dashboardBody(
    tags$head(tags$link(rel = "stylesheet", type = "text/css", href = "custom.css")),
    tabItems(
      tabItem(tabName = "tab",
        div(
          uiOutput('panelTable')
        ),
        div(
          rpivotTableOutput("pivotTable2")
        )
      )
    )
  )
)


# Create Shiny object
shinyApp(ui = ui, server = server)

CSS:

/* Adjust css of pivot table */
#pivotTable{ 
  overflow-x: scroll; 
  overflow-y: scroll;
}

.pvtRows, .pvtCols {
  background: #FAFAFA none repeat scroll 0 0;
}

table.pvtTable tbody tr th, table.pvtTable thead tr th {
  background: #FFFFFF;
}

.pvtAxisContainer li span.pvtAttr {
    background: rgba(147,255,53,0.8);
}

2 个答案:

答案 0 :(得分:1)

你的问题是你的css是否被生成的css规则推翻为pivotTable过度规则,在每个规则之后添加!important

#pivotTable{ 
  overflow-x: scroll; 
  overflow-y: scroll;
}

.pvtRows, .pvtCols {
  background: #FAFAFA none repeat scroll 0 0 !important;
}

table.pvtTable tbody tr th, table.pvtTable thead tr th {
  background: #FFFFFF!important;
}

.pvtAxisContainer li span.pvtAttr {
    background: rgba(147,255,53,0.8) !important;
}

希望这有帮助!

答案 1 :(得分:0)

我认为你可以尝试在div中定义类。

例如:

div(class = "pvtRows pvtAxisContainer",
  uiOutput('panelTable')
)