【问题标题】:Resolving warning in as.datatable in a renderDatatable解决 renderDatatable 中 as.datatable 中的警告
【发布时间】:2021-12-11 14:31:53
【问题描述】:

我有这个闪亮的应用程序,我想在它下面解决警告。我收到的当前警告是Warning in processWidget(instance) : renderDataTable ignores ... arguments when expr yields a datatable object; see ?renderDataTable。我知道这是由于这里提到的renderDataTable() 调用中的as.datatable() 调用:R shiny widgetFunc() warning messages with eventReactive(warning 1) and renderDataTable (warning 2)

我想知道的是如何更改下面的应用程序,使其不再出现警告,但仍保留单元格上的格式?如何最好地重新安排它并不完全清楚。

library(shiny)
library(shinydashboard)
library(DT)
library(formattable)


custom_color_picker <- function(x){
  
  sapply(x,function(x){
    if(x > 0){
      formattable::csscolor("#B7D1DA", format = "hex")
    } else {
      formattable::csscolor("#D38591", format = "hex")
    }
  }
  )
}

paddedcolor_bar <- function(color = "lightgray", fun = "proportion", fun2 = "custom_color_picker", ...) {
  fun <- match.fun(fun)
  fun2 <- match.fun(fun2)
  formatter("span",
            style = function(x) style(
              display = "inline-block",
              direction = "rtl",
              "unicode-bidi" = "plaintext",
              "border-radius" = "4px",
              "padding-right" = "2px",
              "background-color" = fun2(as.numeric(x), ...),
              width = sprintf("%010.4f%%", 100 * percent(fun(as.numeric(x), ...)))
            ))
}

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    DT::dataTableOutput("tabOut")   
  )
) 

server <- function(input, output) {


  output$tabOut <- DT::renderDataTable({
    
    tab <- data.frame(A = -5:20, B = runif(26,0,10), C = letters)
    tab[, 1] <- as.numeric(tab[, 1]) # to be sure it's numerical
    

    as.datatable(
      formattable(tab, 
                  list("A"  = paddedcolor_bar("lightblue"),
                       "B" = formatter("span", x ~ sprintf("%10.2f", x, rank(-x)))))
    )
  }, server = FALSE, plugins = 'natural', options = list(
    columnDefs = list(list(type = "natural", targets = "_all")))
  )

  
}

shinyApp(ui, server)

由reprex package (v2.0.0) 于 2021 年 10 月 26 日创建

【问题讨论】:

    标签: r shiny dt formattable


    【解决方案1】:

    正如警告中提到的renderDataTable ignores ... arguments when expr yields a datatable object - 因此,您需要将... 对象直接传递给datatable 函数,或者在这种情况下传递给as.datatable 函数:

    library(shiny)
    library(shinydashboard)
    library(DT)
    library(formattable)
    
    custom_color_picker <- function(x) {
      sapply(x, function(x) {
        if (x > 0) {
          formattable::csscolor("#B7D1DA", format = "hex")
        } else {
          formattable::csscolor("#D38591", format = "hex")
        }
      })
    }
    
    paddedcolor_bar <-
      function(color = "lightgray",
               fun = "proportion",
               fun2 = "custom_color_picker",
               ...) {
        fun <- match.fun(fun)
        fun2 <- match.fun(fun2)
        formatter(
          "span",
          style = function(x)
            style(
              display = "inline-block",
              direction = "rtl",
              "unicode-bidi" = "plaintext",
              "border-radius" = "4px",
              "padding-right" = "2px",
              "background-color" = fun2(as.numeric(x), ...),
              width = sprintf("%010.4f%%", 100 * percent(fun(
                as.numeric(x), ...
              )))
            )
        )
      }
    
    ui <- dashboardPage(dashboardHeader(),
                        dashboardSidebar(),
                        dashboardBody(DT::dataTableOutput("tabOut")))
    
    server <- function(input, output) {
      
      output$tabOut <- DT::renderDataTable({
        tab <- data.frame(A = -5:20,
                          B = runif(26, 0, 10),
                          C = letters)
        tab[, 1] <- as.numeric(tab[, 1]) # to be sure it's numerical
        
        as.datatable(
          formattable(tab,
                      list(
                        "A" = paddedcolor_bar("lightblue"),
                        "B" = formatter("span", x ~ sprintf("%10.2f", x))
                      ))
          # ,
          # plugins = 'natural',
          # options = list(columnDefs = list(
          #   list(type = "natural", targets = "_all")
          # ))
        )
      }, server = FALSE)
    }
    
    shinyApp(ui, server)
    

    这里只有server 不是... 的renderDataTable 参数。

    【讨论】:

    • 如果您查看我的示例与您的示例,您会发现现在“A”列中的排序不同。我的给出了预期的 -5,-4,-3,-2,-1,0,1,2 而你的给出的是 -5,-4,-3,-2,-1,0,20,19 这不是正确。
    • 不确定是否相关,但您的formatter 呼叫有问题。 sprintf 仅使用提供的两个参数之一。
    • 请看我的编辑。这是您提供的columnDefs 的问题(数字按字母顺序排序)。在您的初始代码中,columnDefs 未应用,因为您在错误的位置传递了它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-29
    • 1970-01-01
    • 2016-07-05
    • 2014-04-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多