无法在Shiny DT中隐藏列并设置行名= FALSE

时间:2019-02-25 21:14:19

标签: r shiny dt

我在Shiny中创建了一个数据表,该数据表使用DT基于一组隐藏列中的值来设置值的样式。该表显示公司的部门是否达到了呼叫和电子邮件的目标。

问题在于,当我隐藏列(使用columnDefs = list(list(targets = c(4, 5), visible = FALSE)))时,无法再在rownames = FALSE调用下使用datatable():该表显示为无数据。有谁知道我如何才能同时使用这两个选项?

我使用了以下文章:

https://rstudio.github.io/DT/010-style.html

How do I suppress row names when using DT::renderDataTable in R shiny?

library(shiny)
library(tidyverse)
library(DT)

x <- tibble(
  Unit = c("Sales", "Marketing", "HR"), 
  Calls = c(100, 150, 120), 
  Emails = c(200, 220, 230), 
  Calls_goal = c(1, 0, 0), 
  Emails_goal = c(0, 1, 1)
)


ui <- fluidPage(

   mainPanel(
         DT::dataTableOutput("table")
      )
)

server <- function(input, output) {

   output$table <- DT::renderDataTable({

     # Can't use both visible = FALSE and rownames = FALSE

     datatable(x, 
               options = list(
                 columnDefs = list(list(targets = c(4, 5), visible = FALSE)) # THIS
              ), 
              rownames = TRUE) %>% # OR THIS
       formatStyle(
         columns = c('Calls', 'Emails'), 
         valueColumns = c('Calls_goal', 'Emails_goal'), 
         color = styleEqual(c(1, 0), c("red", "black"))
       ) 

   })
}

shinyApp(ui = ui, server = server)

1 个答案:

答案 0 :(得分:1)

由于行名也是一列,因此将它们设置为false时,您需要为要隐藏的列重新索引。因此,在您的特定情况下,第5列不再存在。现在它是4号,而4号是3号,因此您的代码应如下所示:

def sum_vals(num1, num2):
    result = sum(range(num1, num2))
    print(result)

sum_vals(3, 7)