【问题标题】:Add an "all" option under the filter that selects the number of rows displayed in a datatable在选择数据表中显示的行数的过滤器下添加“全部”选项
【发布时间】:2019-08-16 11:36:56
【问题描述】:

我有一个带有数据表的基本闪亮应用程序,如下所示。我想知道是否可以在选择要查看的行数的过滤器下添加“全部”选项。我想我可以只添加mpg 数据集的总行数,但我想添加一个标签“全部”。

#ui.r
# Load the ggplot2 package which provides
# the 'mpg' dataset.
library(ggplot2)

fluidPage(
  titlePanel("Basic DataTable"),

  # Create a new Row in the UI for selectInputs
  fluidRow(
    column(4,
        selectInput("man",
                    "Manufacturer:",
                    c("All",
                      unique(as.character(mpg$manufacturer))))
    ),
    column(4,
        selectInput("trans",
                    "Transmission:",
                    c("All",
                      unique(as.character(mpg$trans))))
    ),
    column(4,
        selectInput("cyl",
                    "Cylinders:",
                    c("All",
                      unique(as.character(mpg$cyl))))
    )
  ),
  # Create a new row for the table.
  DT::dataTableOutput("table")
)

#server.r
# Load the ggplot2 package which provides
# the 'mpg' dataset.
library(ggplot2)

function(input, output) {

  # Filter data based on selections
  output$table <- DT::renderDataTable(DT::datatable({
options = list(pageLength = 5,
                   lengthMenu = c(5, 10, 15, 20))
    data <- mpg
    if (input$man != "All") {
      data <- data[data$manufacturer == input$man,]
    }
    if (input$cyl != "All") {
      data <- data[data$cyl == input$cyl,]
    }
    if (input$trans != "All") {
      data <- data[data$trans == input$trans,]
    }
    data
  }))

}

【问题讨论】:

  • 也许我错过了什么,但我们可以做到if(any(input$man=="All",input$cyl=="All",input$trans=="All")) data else{the 3 consitions}

标签: r dt


【解决方案1】:

这对我有用:(基于this

server 中对您的代码进行了更改,并在下面进行了说明。我添加了server &lt;-ui &lt;- 以便能够在本地为我运行它

#ui.r
# Load the ggplot2 package which provides
# the 'mpg' dataset.
library(ggplot2)
library(shiny)
library(DT)

ui <- fluidPage(
  titlePanel("Basic DataTable"),

  # Create a new Row in the UI for selectInputs
  fluidRow(
    column(4,
           selectInput("man",
                       "Manufacturer:",
                       c("All",
                         unique(as.character(mpg$manufacturer))))
    ),
    column(4,
           selectInput("trans",
                       "Transmission:",
                       c("All",
                         unique(as.character(mpg$trans))))
    ),
    column(4,
           selectInput("cyl",
                       "Cylinders:",
                       c("All",
                         unique(as.character(mpg$cyl))))
    )
  ),
  # Create a new row for the table.
  DT::dataTableOutput("table")
)

#server.r
# Load the ggplot2 package which provides
# the 'mpg' dataset.

server <- function(input, output) {

  # Filter data based on selections
  output$table <- DT::renderDataTable({
    data <- mpg
    if (input$man != "All") {
      data <- data[data$manufacturer == input$man,]
    }
    if (input$cyl != "All") {
      data <- data[data$cyl == input$cyl,]
    }
    if (input$trans != "All") {
      data <- data[data$trans == input$trans,]
    }
    data <- DT::datatable(data=data, options = list(pageLength = 5,lengthMenu = list(c(5,10,15,20, -1), list('5', '10', '15','20', 'All')), paging = T))
  })

}

shinyApp(ui = ui, server = server)

首先要注意的是将DT::datatable 分配给您的data 变量。
结合使用 -1 选择所有行并使用 lengthMenu() 作为值向量和名称列表,就可以了。

【讨论】:

  • @A.Suliman 你是对的。我现在改变了事件的顺序,这是固定的。如果它不断跳回number of rows displayed = 5,我仍然不太高兴。但这可以通过反应设置来解决,而不是像现在这样硬编码5
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多