【问题标题】:Possible to only show first and last X rows in R Shiny只能在 R Shiny 中显示第一行和最后 X 行
【发布时间】:2018-04-17 00:42:27
【问题描述】:

是否有可能以某种方式使用闪亮的渲染数据表只显示第一行和最后一行?如果表是有序的,则会更改。

对于这个例子

if (interactive()) {
  shinyApp(
    ui = fluidPage(
      fluidRow(
        column(12,
               dataTableOutput('table')
        )
      )
    ),
    server = function(input, output) {
      output$table <- renderDataTable(data.frame(A = 1:20, B = 20:1),
                                      options = list(
                                        pageLength = 10
                                      )
      )
    }
  )
}

所需的输出输出将是

    A  B
1   1 20
20 20  1

如果用户按 B 订购

    A  B
1  20 1
20 1  20

【问题讨论】:

  • 为什么不直接做df&lt;-data.frame(A = 1:20, B = 20:1) df[c(1,nrow(df)),]
  • 由于原表的列多于2列,所以该方案不起作用。
  • 你需要第一行和最后一行吗?为什么列很重要?该代码基本上允许您显示所有列,而不是 2 列
  • 假设您有 data.frame(A = 1:20, B = 20:1, C = c(rep(1, 9), 20, 0, rep(1, 0)). 如果你只选择first和last,然后想按C排序,它的first和last只有1个值,而实际上有20个,0是原来的df。
  • 您能否将订购项目添加到原始帖子中以阐明您希望该行为如何?

标签: r shiny


【解决方案1】:

您可以在反应式空间中操作数据表,以创建您想要的任何行为。在这里,我在对 df 对象进行排序/重新排序后获取它的头部和尾部:

if (interactive()) {
  library(shiny)
  shinyApp(
    ui = fluidPage(
      fluidRow(
        column(3,
               selectInput("ordering", "How to order", choices = c("A", "B"), selected = "A")),
        column(9,
               dataTableOutput('table')
        )
      )
    ),
    server = function(input, output) {
      output$table <- renderDataTable({ 
        df <- data.frame(A = 1:20, B = 20:1)

        df <- df[order(df[[input$ordering]]), ]

        df_new <- rbind(head(df,1), tail(df,1))
        df_new
        },
        options = list(pageLength = 10))
    }
  )
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-23
    • 1970-01-01
    相关资源
    最近更新 更多