通过更改一个单元格值来更新 rhandsontable
Update rhandsontable by changing one cell value
我有以下数据框:
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))
我想做的是,当我 select 与(仅)第一列的可用值不同时,如果这一行当然存在,整个 table 应该相应地更新。
您需要将 newdata 数据帧和 postcode 指定为反应值,并使用此反应值触发渲染函数。一个工作示例。
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)
希望对您有所帮助!
我有以下数据框:
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))
我想做的是,当我 select 与(仅)第一列的可用值不同时,如果这一行当然存在,整个 table 应该相应地更新。
您需要将 newdata 数据帧和 postcode 指定为反应值,并使用此反应值触发渲染函数。一个工作示例。
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)
希望对您有所帮助!