【问题标题】:'data' must be 2-dimensional (e.g. data frame or matrix) while using DT format R Shiny使用 DT 格式 R Shiny 时,“数据”必须是二维的(例如数据框或矩阵)
【发布时间】:2018-07-30 14:11:35
【问题描述】:

我正在尝试使用 DT 格式化输出。下面是我获取数据框的代码

> MyData
Tab       Classification_Level    Month_considered   Primary_family Secondary_family Metric_name Metric_Value
1 Level1         Brand family           Apr-17          NP              <NA>        <NA>          34,585
2 Level1      Brand Subfamily           Apr-17          PM             <NA>        <NA>           7,401
3 Level1      Brand Subfamily           Apr-17          NP             <NA>        <NA>           34,596
4 Level1      Brand Subfamily           Apr-17         MB NP           <NA>        <NA>           15,985
5 Level1      Brand Subfamily           Apr-17         MB M            <NA>        <NA>           9,712
6 Level1                Brand           Apr-17         KL              <NA>        <NA>           6,242
> sapply(MyData,class)
         Tab Classification_Level     Month_considered       Primary_family     Secondary_family 
    "factor"             "factor"             "factor"             "factor"             "factor" 
 Metric_name         Metric_Value 
    "factor"

 a <- reactive({
    MyData %>%
      filter(
        Classification_Level == input$selected_class &
          Primary_family == input$selected_product &
          Metric_name == input$selected_metric
      ) %>%
      mutate(`ATC_Count` = Metric_Value) %>%
      mutate(`pct` = as.numeric(as.character(Metric_Value)) * 100) %>%
      select(Month_considered, `pct`) %>%
      group_by(Month_considered)
  })

当我检查这个 data.frame 的类时

List of 8
 $ x            :List of 5
  ..$ filter   : chr "none"
  ..$ data     :'data.frame':   11 obs. of  3 variables:
  .. ..$                 : chr [1:11] "1" "2" "3" "4" ...
  .. ..$ Month_considered: Date[1:11], format: "2017-04-01" "2017-05-01" "2017-06-01" "2017-07-01" ...
  .. ..$ pct             : num [1:11] 33.4 34.8 36.6 36.6 34.6 ...

现在在尝试格式化和突出显示列时出现错误'data' must be 2-dimensional (e.g. data frame or matrix)

```

abc<- reactive ({datatable(a()) %>% formatStyle(
    'pct',
    backgroundColor = styleEqual(c(0, 1), c('gray', 'yellow'))
  )})

 output$op1 <- renderDataTable({
    DT::datatable(abc())

  })

【问题讨论】:

  • 寻求帮助时,您应该包含一个简单的reproducible example,其中包含可用于测试和验证可能解决方案的示例输入和所需输出。 (使用dput() 而不是str() 并清楚您共享的是MyData 还是反应转换的结果)。您的数据看起来一团糟,目前还不清楚它究竟是如何变成这样的,
  • 添加了 MyData @MrFlick
  • 这不是可重现的格式。请参阅我提供的链接,了解将数据添加到您的问题的正确方法。那你显示str() 的对象是什么?
  • 哦,问题是你给DT::datatable()打了两次电话吗?如果你只是做了output$op1 &lt;- renderDataTable({abc()})呢?
  • @MrFlick 两次调用 DT:datatable() 是问题所在。谢谢。

标签: r shiny dt


【解决方案1】:

如果 data.table 定义没有问题,那么问题似乎是渲染部分。 'renderDataTable' 在闪亮的和 DT 包中都有。可以试试 DT::renderDataTable 吗?

【讨论】:

    猜你喜欢
    • 2021-07-17
    • 2023-04-06
    • 2019-08-30
    • 1970-01-01
    • 2014-02-26
    • 2021-06-17
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多