我有一个shiny 的应用程序,它正在使用mtars数据集.我正在想出一种按级别对汽车进行排序的方法.因此,如果同一层中有四种类型的汽车,用户可以将rhandsontable编辑为1-1-1-1,而不是简单地编辑1-2-3-4.

排序表按钮和从表中删除行按钮各自工作.然而,我的问题是,如果用户try 实现层(类似于1-1-1-1),但随后决定从rhandsontable中删除一行,则整个表将从1重新排序到N.

有没有一种方法可以确保,如果用户决定对表进行排序,那么如果他们还决定删除一行,那么该表将根据已排序的内容进行重新排序,而不仅仅是总的重新排序?

谢谢


library(shiny)
library(rhandsontable)
library(shinyjs)
library(dplyr)

cars_data <- mtcars %>%
  mutate(tiers = row_number()) %>%
  relocate(tiers, .before = mpg)

shinyApp(
  ui = fluidPage(
    useShinyjs(),
    helpText("Edit the table values in the 'Tiers' column to sort the table."),
    actionButton(inputId = "sort_button", label = "Sort Table"),
    actionButton(inputId = "remove_row_button", label = "Remove Row From Table", disabled = ''),
    br(),
    br(),
    rHandsontableOutput("cars_table")
  ),
  
  server = function(input, output, session) {
    
    
    cars_rv <- reactiveValues(
      table = cars_data,
      original_order = 1:nrow(cars_data)
    )
    
    output$cars_table <- renderRHandsontable({
      rhandsontable(data = cars_rv$table,
                    selectCallback = TRUE) %>%
        hot_col("mpg", colWidths = 75, readOnly = T) %>%
        hot_col("cyl", colWidths = 75, readOnly = T) %>%
        hot_col("disp", colWidths = 90, readOnly = T) %>%
        hot_col("hp", colWidths = 90, readOnly = T) %>%
        hot_col("drat", colWidths = 75, readOnly = T) %>%
        hot_col("wt", colWidths = 75, readOnly = T) %>%
        hot_col("qsec", colWidths = 90, readOnly = T) %>%
        hot_col("vs", colWidths = 75, readOnly = T) %>%
        hot_col("am", colWidths = 75, readOnly = T) %>%
        hot_col("gear", colWidths = 75, readOnly = T) %>%
        hot_col("carb", colWidths = 75, readOnly = T)
      
    })
    
    
    observe({
      if (!is.null(input$cars_table_select$select$r)) {
        shinyjs::enable("remove_row_button")
      }
    })
    
    
    observeEvent(input$remove_row_button, {
      selected_rhands_rows <- input$cars_table_select$select$r
      cars_rv$table <- cars_rv$table %>%
        slice(-c(selected_rhands_rows))
      
      cars_rv$table <- cars_rv$table %>%
        mutate(tiers = row_number()) %>%
        arrange(match(tiers, cars_rv$original_order))
      
      output$cars_table <- renderRHandsontable({
        rhandsontable(data = cars_rv$table,
                      selectCallback = TRUE) %>%
        hot_col("mpg", colWidths = 75, readOnly = T) %>%
        hot_col("cyl", colWidths = 75, readOnly = T) %>%
        hot_col("disp", colWidths = 90, readOnly = T) %>%
        hot_col("hp", colWidths = 90, readOnly = T) %>%
        hot_col("drat", colWidths = 75, readOnly = T) %>%
        hot_col("wt", colWidths = 75, readOnly = T) %>%
        hot_col("qsec", colWidths = 90, readOnly = T) %>%
        hot_col("vs", colWidths = 75, readOnly = T) %>%
        hot_col("am", colWidths = 75, readOnly = T) %>%
        hot_col("gear", colWidths = 75, readOnly = T) %>%
        hot_col("carb", colWidths = 75, readOnly = T)
      })
      
      shinyjs::disable("remove_row_button")
      
    })
    
    
    observeEvent(input$sort_button, {
      edited_data <- hot_to_r(input$cars_table)
      edited_data <- edited_data[order(edited_data$tiers), ]
      cars_rv$table <- edited_data
      cars_rv$original_order <- 1:nrow(cars_rv$table)
    })
  }
)

推荐答案

remove_row_buttonobserveEvent内,您可以替换

cars_rv$table <- cars_rv$table %>%
        mutate(tiers = row_number()) %>%
        arrange(match(tiers, cars_rv$original_order))

使用

cars_rv$table <- cars_rv$table |> 
                mutate(tiers = dense_rank(tiers))

这应该做的工作:

enter image description here

还请注意,在这observeEvent内,您应该使用

cars_rv$table <- hot_to_r(input$cars_table) |> 
                slice(-c(selected_rhands_rows))

使得如果用户事先没有点击排序按钮,则重新排序也起作用.

R相关问答推荐

高质量地将R格式的图表从Word中输出

删除facet_wrap标签之间的水平线

检测(并替换)字符串中的数学符号

如何使用rmarkdown和kableExtra删除包含折叠行的表的第一列的名称

无法将传奇添加到cowplot多情节中

在数学中正确显示摄氏度、开氏度或华氏度

我想在R中总结一个巨大的数据框架,使我只需要唯一的lat、lon、Date(Year)和Maxium Value""""""""

如何得到R中唯一的组合群?

如何改变时间图R中的悬停信息?

bslib::card_header中的shine::downloadButton,图标而不是文本

如何优化向量的以下条件赋值?

如何通过判断数据框的一列来压缩另一列?

迭代到DataFrame列并获得成对的值列表(col1->;col2、col2->;col3、col3->;col4等)的正确方法.

如何将SAS数据集的列名和列标签同时包含在r中GT表的表首?

如何根据R中其他变量的类别汇总值?

按组内中位数分类

在R中的数据框上使用Apply()函数时,如何保留非数字列?

如何使用循环从R中的聚合函数创建列,而不会在名称中给出&q;$&q;?

网络抓取NBA.com

创建由三个单独的shapefile组成的单个 map