2

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

我使用 conditionPanels (有时)根据 object 的类型显示数据透视表df

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

当没有第二个数据透视表时,如何确保样式表用于第一个数据透视表?

# 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);
}
4

2 回答 2

1

嗨,您的问题是,您的 css 已被生成的 css 规则推翻为数据透视表,从而!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;
}

希望这可以帮助!

于 2017-12-21T22:07:30.600 回答
0

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

例如:

div(class = "pvtRows pvtAxisContainer",
  uiOutput('panelTable')
)
于 2017-12-21T17:19:52.747 回答