javascript 重置rhandsontable中的选定范围

6yoyoihd  于 2023-03-21  发布在  Java
关注(0)|答案(1)|浏览(110)

bounty将在4天后过期。回答此问题可获得+50声望奖励。mat希望引起更多人关注此问题。

在下面的示例中,每次生成新数据集时,如何重置选定的单元格区域?

library(shiny)
library(data.table)
library(rhandsontable)

shinyApp(
  ui = fluidPage(
    actionButton('gendata', 'Generate data'),
    hr(),
    verbatimTextOutput('verb'),
    hr(),
    rHandsontableOutput('table_original'),
    hr(),
    rHandsontableOutput('table_subset')
  ),
  
  server = function(input, output, session) {
    mydata <- reactive({
      req(input$gendata)
      n_dim <- sample(2:8, 1)
      data.table( matrix(rnorm(n_dim**2), n_dim, n_dim) )
    })
    output$table_original <- renderRHandsontable({
      req(mydata())
      rhandsontable(data = mydata(), selectCallback = TRUE)
    })
    output$table_subset <- renderRHandsontable({
      req(input$table_original_select$select)
      selected_rows <- input$table_original_select$select$rAll
      selected_cols <- input$table_original_select$select$cAll
      DT <- hot_to_r(input$table_original)
      DT <- DT[selected_rows, ..selected_cols] # subset
      rhandsontable(DT)
    })
    output$verb <- renderPrint({
      str(input$table_original_select$select)
    })
  }
)

编辑1

我想一个解决方案可能是使用javascript,但我对这种语言不太熟悉,所以无法解决这个问题。

编辑2

我设法避免了当生成新数据集时发生的错误,并且以前的范围大于当前数据集。我仍然希望可以有更好的解决方案。

library(shiny)
library(data.table)
library(rhandsontable)

shinyApp(
  ui = fluidPage(
    actionButton('gendata', 'Generate data'),
    hr(),
    verbatimTextOutput('verb'),
    hr(),
    rHandsontableOutput('table_original'),
    hr(),
    rHandsontableOutput('table_subset')
  ),
  
  server = function(input, output, session) {
    
    first_range_selection <<- TRUE
    
    mydata <- reactive({
      req(input$gendata)
      n_dim <- sample(2:8, 1)
      data.table( matrix(rnorm(n_dim**2), n_dim, n_dim) )
    })
    
    observeEvent(mydata(), {
      if (first_range_selection) {
        reset_range <<- FALSE
        first_range_selection <<- FALSE
      } else {
        reset_range <<- TRUE
      }
    })
    
    output$table_original <- renderRHandsontable({
      req(mydata())
      rhandsontable(data = mydata(), selectCallback = TRUE)
    })
    output$table_subset <- renderRHandsontable({
      req(input$table_original_select$select)
      if (reset_range) {
        reset_range <<- FALSE
        return()
      }
      selected_rows <- input$table_original_select$select$rAll
      selected_cols <- input$table_original_select$select$cAll
      DT <- hot_to_r(input$table_original)
      DT <- DT[selected_rows, ..selected_cols] # subset
      rhandsontable(DT)
    })
    output$verb <- renderPrint({
      str(input$table_original_select$select)
    })
  }
)
zte4gxcn

zte4gxcn1#

您可以对两个observeEvent进行编程:

  • input$gendata处将output$table_subset重置为NULL
  • input$table_original_select处创建output$table_subset的方法

这样,它在我这边工作得很好:

library(shiny)
library(data.table)
library(rhandsontable)

shinyApp(
  ui = fluidPage(
    actionButton('gendata', 'Generate data'),
    hr(),
    verbatimTextOutput('verb'),
    hr(),
    rHandsontableOutput('table_original'),
    hr(),
    rHandsontableOutput('table_subset')
  ),
  
  server = function(input, output, session) {
    
    mydata <- reactive({
      req(input$gendata)
      n_dim <- sample(2:8, 1)
      data.table( matrix(rnorm(n_dim**2), n_dim, n_dim) )
    })
    
    output$table_original <- renderRHandsontable({
      req(mydata())
      rhandsontable(data = mydata(), selectCallback = TRUE)
    })
    
    observeEvent(input$gendata, {
      output$table_subset <- NULL
    })
    
    observeEvent(input$table_original_select, {
      output$table_subset <- renderRHandsontable({
        req(input$table_original_select$select)
        selected_rows <- input$table_original_select$select$rAll
        selected_cols <- input$table_original_select$select$cAll
        DT <- hot_to_r(input$table_original)
        DT <- DT[selected_rows, ..selected_cols] # subset
        rhandsontable(DT)
      })
    })
    
    output$verb <- renderPrint({
      str(input$table_original_select$select)
    })
  }
)

相关问题