【问题标题】:Select the number of rows to display in a datatable based on a slider input根据滑块输入选择要在数据表中显示的行数
【发布时间】:2018-11-27 11:30:55
【问题描述】:

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

这是我的应用,pageLength 为 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() 
                         )
        )
      )
    
    }
    

【问题讨论】:

  • 你快到了。使用input$rows 而不是sliderInput$rows
  • 好的。感谢您的帮助。

标签: r shiny dt


【解决方案1】:

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

服务器

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

    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-12
    • 2016-06-03
    • 1970-01-01
    • 1970-01-01
    • 2023-01-27
    • 2021-10-24
    • 2021-10-14
    • 1970-01-01
    相关资源
    最近更新 更多