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

通过更改一个单元格值更新rhandsontable

  •  0
  • firmo23  · 技术社区  · 5 年前

    我有下面的数据帧:

    DF2 = data.frame(agency_postcode = factor(rep(c(12345,45678,24124,32525,32325),2)),
                     car_group=factor(rep(c("Microcar","City car","Supermini","Compact","SUV"),2)),
                     transmission=factor(rep(c("automatic","manual"),5)))
    

    newdata <- DF2[ which(DF2$agency_postcode =='12345'), ]
    

    以及重新分解,以便相应地将第二列和第三列的下拉值设置为仅在子集之后可用的值

    for(i in 2:ncol(newdata)){
      newdata[,i] <- factor(newdata[,i])
    }
    

    library(rhandsontable)
    rhandsontable(newdata[1,], rowHeaders = NULL, width = 550, height = 300)%>%
      hot_col(colnames(newdata))   
    

    0 回复  |  直到 5 年前
        1
  •  1
  •   FAlonso    5 年前

    你需要分配 新数据 数据帧和

    library(shiny)
    library(rhandsontable)
    
    
    ui <- fluidPage(
    
       titlePanel("RHandsontable"),
       sidebarLayout(
          sidebarPanel(),
          mainPanel(
             rHandsontableOutput("test")
          )
       )
    )
    
    
    server <- function(input, output) {
    
      # Assign value of 12345 as default to postcode for the default table rendering
      values <- reactiveValues(postcode = "12345",tabledata = data.frame())
    
      # An observer which will check the value assigned to postcode variable and create the sample dataframe
      observeEvent(values$postcode,{
        DF2 = data.frame(agency_postcode = factor(rep(c(12345,45678,24124,32525,32325),2)),
                         car_group=factor(rep(c("Microcar","City car","Supermini","Compact","SUV"),2)),
                         transmission=factor(rep(c("automatic","manual"),5)))
      # Created dataframe is assigned to a reactive dataframe 'tabledata'
        values$tabledata <- DF2[ which(DF2$agency_postcode ==values$postcode), ]
        for(i in 2:ncol(values$tabledata)){
          values$tabledata[,i] <- factor(values$tabledata[,i])
        }
      })
    
      # Capture changes made in the first column of table and assign the value to the postcode reactive variable. This would then trigger the previous observer
      observeEvent(input$test$changes$changes,{
        col <- input$test$changes$changes[[1]][[2]]
        if(col==0){
          values$postcode <- input$test$changes$changes[[1]][[4]]
        }
      })
    
     # Use the reactive df 'tabledata' to render.
      output$test <- renderRHandsontable({
        rhandsontable(values$tabledata[1,], rowHeaders = NULL, width = 550, height = 300)%>%
          hot_col(colnames(values$tabledata)) 
      })
    
    
    }
    
    shinyApp(ui = ui, server = server)
    

    希望这有帮助!