r Shiny:选择一行以开始显示数据表

发布于 2025-02-07 13:31:28 字数 1366 浏览 1 评论 0原文

下面有一个闪亮的应用程序,该应用使用DataTable使用dt。而不是让表从第1行开始显示,我想将表渲染使用用户选择的特定顶行(在此示例中使用Input $ startrow)。

例如,如果用户在滑块中选择50,则表中显示的第一行是第50行,而不是第1行。

获得动态起始行的任何提示。

为了清楚地编辑:我不想将表子集划分,我想显示要在input $ startrow中开始,但是用户可以上下滚动并仍然查看整个数据集(例如,> 忠实的在此示例中)。

编辑2:似乎问题是displayStart选项是我想要的,但是有一个已知错误截至5月21日,scroller启动显示中间表。

library(shiny)
library(DT)
data("faithful")

ui <- fluidPage(
  h2("Select the first row of the table to show"),
  sliderInput(inputId="startRow",
              label="Choose a row to start the datatable display",
              min = 1,
              max = 200,
              value = 100,
              step=5),
  # show a datatable
  h2("Show a table"),
  dataTableOutput("table1")
)

server <- function(input, output) {
  output$table1 <- renderDataTable({
    # use input$startRow to begin the table display?
    datatable(faithful,
              extensions = "Scroller", 
              options = list(scrollY = 300,
                             scroller = TRUE))
  })

}

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

below there is a shiny app that renders a datatable using DT. Rather than have the table start the display at row 1 I'd like to have the table render with a specific top row selected by the user (using input$startRow in this example).

E.g., if the user chose 50 in the slider the first row shown in the table would be row 50 rather than row 1.

Any tips for getting a dynamic starting row appreciated.

Edit for clarity: I do not want to subset the table, I want to display to begin at input$startRow but the user could scroll up and down and still see the entire dataset (e.g., faithful in this example).

Edit 2: It looks like the issue is that the displayStart option is what I want but that there is a known bug as of May 21 with Scroller starting the display mid table.

library(shiny)
library(DT)
data("faithful")

ui <- fluidPage(
  h2("Select the first row of the table to show"),
  sliderInput(inputId="startRow",
              label="Choose a row to start the datatable display",
              min = 1,
              max = 200,
              value = 100,
              step=5),
  # show a datatable
  h2("Show a table"),
  dataTableOutput("table1")
)

server <- function(input, output) {
  output$table1 <- renderDataTable({
    # use input$startRow to begin the table display?
    datatable(faithful,
              extensions = "Scroller", 
              options = list(scrollY = 300,
                             scroller = TRUE))
  })

}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

草莓酥 2025-02-14 13:31:28

成功。跟随 link> link> link 评论。我能够使用initcompleteinput $ startrow在行上启动表。这似乎有效。

library(shiny)
library(DT)
data("faithful")

ui <- fluidPage(
  h2("Select the first row of the table to show"),
  sliderInput(inputId="startRow",
              label="Choose a row to start the datatable display",
              min = 1,
              max = 200,
              value = 10,
              step=5),
  # show a datatable
  h2("Show a table"),
  dataTableOutput("table1")
)

server <- function(input, output) {
  output$table1 <- renderDataTable({
    datatable(faithful,
              extensions = "Scroller",
              options = list(scrollY = 300,
                             scroller = TRUE,
                             initComplete  = JS('function() {this.api().table().scroller.toPosition(',
                                                input$startRow-1,');}')))})

}

shinyApp(ui = ui, server = server)

Success. Following the link in the comment. I was able to use initComplete to start the table on the row from input$startRow. This appears to work.

library(shiny)
library(DT)
data("faithful")

ui <- fluidPage(
  h2("Select the first row of the table to show"),
  sliderInput(inputId="startRow",
              label="Choose a row to start the datatable display",
              min = 1,
              max = 200,
              value = 10,
              step=5),
  # show a datatable
  h2("Show a table"),
  dataTableOutput("table1")
)

server <- function(input, output) {
  output$table1 <- renderDataTable({
    datatable(faithful,
              extensions = "Scroller",
              options = list(scrollY = 300,
                             scroller = TRUE,
                             initComplete  = JS('function() {this.api().table().scroller.toPosition(',
                                                input$startRow-1,');}')))})

}

shinyApp(ui = ui, server = server)

余罪 2025-02-14 13:31:28

是的,使用输入$ startrow开始表显示以生成所选表。

library(shiny)
library(DT)
data("faithful")

ui <- fluidPage(
  h2("Select the first row of the table to show"),
  sliderInput(inputId="startRow",
              label="Choose a row to start the datatable display",
              min = 1,
              max = 200,
              value = 100,
              step=5),
  # show a datatable
  h2("Show a table"),
  dataTableOutput("table1")
)

server <- function(input, output) {
  
  topDF <- reactive({
    # use input$startRow to begin the table display
    topRow <- input$startRow
    
    selectedDf <- faithful[-(1:(topRow-1)), ]
    
    return(selectedDf)
  })
  
  output$table1 <- renderDataTable({

    datatable(topDF(),
              extensions = "Scroller", 
              options = list(scrollY = 300,
                             scroller = TRUE))
  })
  
}

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

Yes, use input$startRow to begin the table display to generate the selected table.

library(shiny)
library(DT)
data("faithful")

ui <- fluidPage(
  h2("Select the first row of the table to show"),
  sliderInput(inputId="startRow",
              label="Choose a row to start the datatable display",
              min = 1,
              max = 200,
              value = 100,
              step=5),
  # show a datatable
  h2("Show a table"),
  dataTableOutput("table1")
)

server <- function(input, output) {
  
  topDF <- reactive({
    # use input$startRow to begin the table display
    topRow <- input$startRow
    
    selectedDf <- faithful[-(1:(topRow-1)), ]
    
    return(selectedDf)
  })
  
  output$table1 <- renderDataTable({

    datatable(topDF(),
              extensions = "Scroller", 
              options = list(scrollY = 300,
                             scroller = TRUE))
  })
  
}

# Run the application 
shinyApp(ui = ui, server = server)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文