【问题标题】:Update rhandsontable by changing one cell value通过更改一个单元格值来更新 rhandsontable
【发布时间】:2020-01-15 10:50:57
【问题描述】:

我有下面的数据框:

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))   

我想要做的是,当我从(仅)第一列的可用值中选择不同的值时,如果该行当然存在,整个表应该相应地更新。

【问题讨论】:

    标签: r rhandsontable


    【解决方案1】:

    您需要将 newdata 数据框和选择的 邮政编码 指定为响应值,并使用此响应值触发渲染函数。一个工作示例。

    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)
    

    希望这会有所帮助!

    【讨论】:

    • 一个很好的方法
    • 你能解释一下我如何对前两列做同样的事情,什么时候改变也会改变第三列?当然如果存在这样的行
    • 您需要添加另一个观察者来监听第二列中的值变化并再次触发呈现表格。我会尽快发布一个工作代码sn-p。
    • 非常感谢你救了我
    • 基本上我根据您的回答创建了一个更复杂的新问题,因为通过回答它来获得声望点更公平。 stackoverflow.com/questions/57937662/…
    猜你喜欢
    • 2020-01-15
    • 2022-08-14
    • 2022-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-02
    • 1970-01-01
    相关资源
    最近更新 更多