代码之家  ›  专栏  ›  技术社区  ›  Wilcar

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

  •  0
  • Wilcar  · 技术社区  · 8 年前

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

    这是我的应用程序,页面长度为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)
    

    我在服务器端的尝试: 一。首先,我试着用 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 回复  |  直到 8 年前
        1
  •  0
  •   Wilcar    8 年前

    基于Aur?le comment,这里是我更新的服务器端:

    服务器功能(输入、输出){

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