根据滑块输入选择要在数据表中显示的行数

时间:2018-06-18 15:07:48

标签: r shiny dt

我正在尝试使用滑块输入选择要在数据中显示的行数。

这是我的应用使用pageLength of 2

library(shiny)
library(DT)

# Dummy data 
dataset <- data.frame(lng = c(-5, -5, -5, -5, -15, -15, -10),
             lat = c(8, 8, 8, 8, 33, 33, 20),
             year = c(2018, 2018, 2018, 2017, 2017, 2017, 2016),
             type = c('A', 'A', 'A', 'A', 'B', 'B', 'A'),
             id =c("1", "1", "1", "1", "2", "2", "3"))


ui <- fluidPage(

  sidebarLayout(
  sidebarPanel(
     sliderInput("rows",
                 "Number of rows",
                 min = 1,
                 max = 50,
                 value = 1)
  ),

 # datable output 
     DT::dataTableOutput(outputId =  "table")
  )
 )
)

 server <- function(input, output) {



        output$table <- DT::renderDataTable(
            dat <- datatable(dataset,
                             options = list(
                               paging =TRUE,
                               pageLength =  2 
                               )
                             )
            )

        }

        # Run the application 
        shinyApp(ui = ui, server = server)

我在服务器端试了一下: 1.首先,我尝试使用sliderInput$rows作为pageLength的参数:pageLength = sliderInput$rows

  1. 我也试过服务器端:

     server <- function(input, output) {
    
      i <- reactive({sliderInput$rows})
    
      output$table <- DT::renderDataTable(
        dat <- datatable(dataset,
                         options = list(
                           paging =TRUE,
                           pageLength =  i() 
                         )
        )
      )
    
    }
    

1 个答案:

答案 0 :(得分:0)

基于Aurèle的评论,这是我更新的服务器端:

服务器<-功能(输入,输出){

    output$table <- DT::renderDataTable(
        dat <- datatable(dataset,
                         options = list(
                           paging =TRUE,
                           pageLength =  input$rows 
                           )
                         )
        )

    }
相关问题