【问题标题】:Change the color accordingly相应地更改颜色
【发布时间】:2019-09-01 08:29:27
【问题描述】:

请执行以下代码并在主题选择器中选择深色,然后表格中的数字消失,因为数字的颜色在黑暗中是白色的,但背景也是白色的。

library(rhandsontable)
library(shiny)

editTable <- function(DF, outdir=getwd(), outfilename="table"){
    ui <- shinyUI(fluidPage(



        shiny::tags$head(
            shinythemes::themeSelector(),

            shiny::tags$style(shiny::HTML("




.ui-draggable {
 z-index: 3;
 background-color: #CCCCFF;

    "))

        ),#taghead




        titlePanel("Edit and save a table"),
        sidebarLayout(
            sidebarPanel(
                helpText("Shiny app based on an example given in the rhandsontable package.", 
                         "Right-click on the table to delete/insert rows.", 
                         "Double-click on a cell to edit"),

                wellPanel(
                    h3("Table options"),
                    radioButtons("useType", "Use Data Types", c("TRUE", "FALSE"))
                ),
                br(), 

                wellPanel(
                    h3("Save"), 
                    actionButton("save", "Save table")
                )        

            ),

            mainPanel(
                shiny::absolutePanel(        draggable = T, style ="red",fixed=TRUE,
                  h3("Table options"),

                rHandsontableOutput("hot"),
                h3("Table options")

                )
            )
        )
    ))

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

        values <- reactiveValues()

        ## Handsontable
        observe({
            if (!is.null(input$hot)) {
                DF = hot_to_r(input$hot)
            } else {
                if (is.null(values[["DF"]]))
                    DF <- DF
                else
                    DF <- values[["DF"]]
            }
            values[["DF"]] <- DF
        })

        output$hot <- renderRHandsontable({
            DF <- values[["DF"]]
            if (!is.null(DF))
                rhandsontable(DF, useTypes = as.logical(input$useType), stretchH = "all")
        })

        ## Save 
        observeEvent(input$save, {
            finalDF <- isolate(values[["DF"]])
            saveRDS(finalDF, file=file.path(outdir, sprintf("%s.rds", outfilename)))
        })

    })

    ## run app 
    runApp(list(ui=ui, server=server))
    return(invisible())
}



editTable(DF=data.frame(a=c(1,2),b=c(3,4)))

下图显示了当主题较暗时单元格中的数字消失。

有什么方法可以避免这种消失吗? 我不知道如何通过 css 避免。

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    这个问题与 Rhandsontable 的包作者为表格定义 CSS 的方式有关。他们已经明确设置了

    的颜色
    1. 表格单元格背景颜色为白色

    2. 标题文本颜色为黑色

    但他们没有明确设置单元格中文本的颜色。所以发生的事情是,shinythemes 正在将文本颜色更改为白色,但明确设置的背景颜色并没有被 shinythemes 覆盖。

    我建议改用dataTableOutput()renderDataTable()

    library(shiny)
    
    DF=data.frame(a=c(1,2),b=c(3,4))
    
    ui <- shinyUI(fluidPage(
      shiny::tags$head(
        shinythemes::themeSelector(),
    
        shiny::tags$style(shiny::HTML("
    
                                      .ui-draggable {
                                      z-index: 3;
                                      }
    
    
                                      "))
    
        ),#taghead
    
    
    
    
      titlePanel("Edit and save a table"),
      sidebarLayout(
        sidebarPanel(
          helpText("Shiny app based on an example given in the rhandsontable package.", 
                   "Right-click on the table to delete/insert rows.", 
                   "Double-click on a cell to edit"),
    
          wellPanel(
            h3("Table options"),
            radioButtons("useType", "Use Data Types", c("TRUE", "FALSE"))
          ),
          br(), 
    
          wellPanel(
            h3("Save"), 
            actionButton("save", "Save table")
          )        
    
        ),
    
        mainPanel(
          shiny::absolutePanel(        draggable = T, style ="red",fixed=TRUE,
                                       h3("Table options"),
    
                                       dataTableOutput("hot"),
                                       h3("Table options")
    
          )
        )
      )
        ))
    
    server <- shinyServer(function(input, output) {
    
      values <- reactiveValues()
    
      ## Handsontable
      observe({
        if (!is.null(input$hot)) {
          DF = hot_to_r(input$hot)
        } else {
          if (is.null(values[["DF"]]))
            DF <- DF
          else
            DF <- values[["DF"]]
        }
        values[["DF"]] <- DF
      })
    
      output$hot <- renderDataTable({
        DF <- values[["DF"]]
        if (!is.null(DF))
          DF
      })
    
      ## Save 
      observeEvent(input$save, {
        finalDF <- isolate(values[["DF"]])
        saveRDS(finalDF, file=file.path(outdir, sprintf("%s.rds", outfilename)))
      })
    
    })
    
      ## run app 
    runApp(list(ui=ui, server=server))
    

    另外,我们不应该将闪亮的应用程序本身放在一个函数中。这在部署到 shinyapps.io 或 Rsconnect 时不起作用。应用程序本身需要位于单个 app.R 文件或单独的 ui.R 和 server.R 文件中。

    【讨论】:

    • 谢谢,.... 但renderDataTable() 无法手动编辑其编号。另一方面,Rhandsontable 可以手动编辑,谢谢你的旁注,当我发布闪亮的代码时,我会单独写。我将尝试为单元格中的数字指定 css,但是,它也会更改 GUI 的所有 css。我想知道有一种方法可以仅在面板中更改颜色 oc 字符。
    • 有一种方法可以只更改表格中文本的颜色,使用更具选择性的 CSS 选择器指定表格的 ID。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多