通过更改一个单元格的值来更新可录音 [英] Update rhandsontable by changing one cell value

查看:158
本文介绍了通过更改一个单元格的值来更新可录音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据框如下:

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

并通过第一列的值之一对其进行子集化

and after subseting it by one of the values of the 1st column

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

并进行重构,以便将第二列和第三列的下拉值设置为仅子集之后的可用值

and re-factoring in order to set accordingly the dropdown values of the second and third column to only available values after the subset

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

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

What I want to do is when I select a different value from the available ones of (only) the 1st column the whole table should be updated accordingly if of course this row exists.

推荐答案

您需要分配 newdata 数据框和邮政编码作为反应值并触发渲染使用此电抗值的功能.一个可行的例子.

You would need to assign the newdata dataframe and the postcode selected as reactive values and trigger the render function using this reactive value. A working example.

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)

希望这会有所帮助!

这篇关于通过更改一个单元格的值来更新可录音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆