在Shiny中更改rpivotTable的颜色 [英] Changing the colors of rpivotTable in Shiny

查看:160
本文介绍了在Shiny中更改rpivotTable的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正拼命尝试更改rpivotTable包的默认颜色.我还在软件包创建者的github上发布了一个问题,但是还没有人回答,因此,如果有人对如何解决此问题有一个想法,我将不胜感激.

I'm desperately trying to change the default colors of the rpivotTable package. I have also posted an issue in the github of the creator of the package, but no one has answered yet, so if someone has an idea how to fix this problem, I'd be more than grateful.

我的主要问题是更改变量选择下方矩形中的蓝色:示例

My main problem is changing the blue colors in the rectangles below the variable choice: Example

借助我在互联网上发现的此功能,我设法更改了整个背景,但没有更改我想要的特定项(仅到目前为止,还不包括Shiny):

With this function that I have found on the internet, I manage to change the whole background, but not the specific thing I want (outside of Shiny only so far):

style_widget <- function(hw=NULL, style="", addl_selector="") {
  stopifnot(!is.null(hw), inherits(hw, "htmlwidget"))

  # use current id of htmlwidget if already specified
  elementId <- hw$elementId
  if(is.null(elementId)) {
    # borrow htmlwidgets unique id creator
    elementId <- sprintf(
      'htmlwidget-%s',
      htmlwidgets:::createWidgetId()
    )
    hw$elementId <- elementId
  }

  htmlwidgets::prependContent(
    hw,
    htmltools::tags$style(
      sprintf(
        "#%s %s {%s}",
        elementId,
        addl_selector,
        style
      )
    )
  )
}
pivot_graph<-rpivotTable(mtcars)
browsable(
  tagList(
    style_widget(hw=pivot_graph, "background-color: rgb(245, 245, 245);", "table td")
  )
)

但是,当我尝试使用Shiny时,我无法弄清楚在哪里放置以及如何放置它(即使使用此功能完全可以做到).任何帮助表示赞赏.到目前为止,我的Shiny代码:

However, when I try to do it Shiny, I can't figure out what to put where and how to do it (or even if it's at all possible with this function). Any help is appreciated. My Shiny code so far:

library(shiny)
library(rpivotTable)
library(rvest)
ui <- fluidPage(
  titlePanel("Cars"),
  sidebarLayout(
    sidebarPanel(
      fileInput('file1', 'Choose CSV File',
                accept=c('text/csv','text/comma-separated-values,text/plain','.csv')),
      actionButton("save1","Save Table 1")
      # actionButton("save2","Save Table 2")
    ),
    mainPanel(
      tabsetPanel(
        tabPanel("Pivot Table 1",
                 rpivotTableOutput("table")),
        tabPanel("Pivot Table 2",
                 rpivotTableOutput("table2"))
      )
    )
  )



)


server <- function(input, output,session)
{

  session$onSessionEnded(stopApp)

  observe({
    file1 = input$file1
    if (is.null(file1)) {
      return(NULL)
    }
    st_data <<- read.csv(file1$datapath)



    output$table <- renderRpivotTable({
      rpivotTable(mtcars,
                  rendererName="Table",
                  onRefresh = htmlwidgets::JS("function(config) {Shiny.onInputChange('myData', 
                                              document.getElementById('table').innerHTML); }")
      )
    })


    output$table2 <- renderRpivotTable({
      rpivotTable(mtcars,aggregatorName="Average",   
                  rendererName="Table",
                  onRefresh = htmlwidgets::JS("function(config) {Shiny.onInputChange('myData', 
                                                  document.getElementById('table').innerHTML); }")
      )
    })

    summarydf <- eventReactive(input$myData,{
      input$myData %>%
        read_html %>%
        html_table(fill = TRUE) %>%
        .[[2]]
    })

    observeEvent(input$save1, {
      if(nrow(summarydf() )<1) return()
      write.csv(summarydf(), file="./cars1.csv")
    })

    # summarydf1 <- eventReactive(input$myData1,{
    #   input$myData1 %>%
    #     read_html %>%
    #     html_table(fill = TRUE) %>%
    #     .[[4]]
    # })
    # 
    # observeEvent(input$save2, {
    #   if(nrow(summarydf1() )<1) return()
    #   write.csv(summarydf1(), file="./cars2.csv")
    # })
    })
}

shinyApp(ui = ui, server = server)

推荐答案

以下代码会将浅蓝色更改为非常深的蓝色.

The following code will change the light blue to a very deep blue.

您可以通过类似的方式更改任何ivot.min.css代码:唯一的挑战就是确定哪个元素是正确的!

You can change any pivot.min.css code in a similar way: the only challenge is to identify which is the right element!

要更改颜色,请搜索JavaScript颜色图,然后将#000080更改为所需的任何颜色.

To change the colour, search for a JavaScript colour map and change #000080 to whatever you need.

library(shiny)

df <- iris

ui <- fluidPage(
            tags$style(type="text/css",".pvtRows, .pvtCols { background: #000080 none repeat scroll 0 0; }" ),

                        fluidRow(
                            column(width=10, rpivotTableOutput("pivot"))
                            )
                        )

server <- function(input, output, session) {

output$pivot<-renderRpivotTable({

            rpivotTable(df,
                rendererName="Heatmap",
                cols=c("Species"),
                rows=c("Petal.Width"),
                aggregatorName="Count"
                )


        })

}

shinyApp(ui = ui, server = server)

如果满足您的要求,请告诉我.

Please let me know if this is addressing your requirement.

这篇关于在Shiny中更改rpivotTable的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆