我有下面的数据框架:
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)))我创建了一个闪亮的应用程序,它的目标是根据第一个rhandsontable.More的修改修改第二个rhandsontable:
1.最初将此DF2的第一行显示为rhandsontable。
2.然后,我通过rhandsontable选择一个下拉选项来修改第一行-第一列的单元格。
3.我通过jsonlite将修改后的rhandsontable转换为数据帧。
4.然后,我从该数据帧中提取修改后的值,并将初始DF2的第一列设置为子集。
5.然后我显示了第二个rhandsontable的第一行,它应该显示修改后的数据集,但它没有。
#ui.r
library(shiny)
library(rhandsontable)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
),
mainPanel(
rHandsontableOutput("hot"),
rHandsontableOutput("hot2")
)
)
)
#server.r
library(shiny)
library(rhandsontable)
library(jsonlite)
server <- function(input, output) {
#Create rhandsontable as a reactive expression
DFR2<-reactive({
rhandsontable(DF2[1,1:2], rowHeaders = NULL,width=1400,height = 200)%>%
hot_col(colnames(DF2)[1])
})
#Display the rhandsontable
output$hot <- renderRHandsontable({
DFR2()
})
#Convert the rhandsontable to a daraframe
DFR3<-reactive({
data_fram <- fromJSON(DFR2()$x$data)
})
#Subset the initial dataframe by value of the 1st row-1st column cell of DF3
DFR4<-reactive({
newdata <- DF2[ which(DF2[,1]==DFR3()[1,1]), ]
})
#Display the new rhandsontable
output$hot2 <- renderRHandsontable({
rhandsontable(DFR4()[1,], rowHeaders = NULL,width=1400,height = 200)%>%
hot_col(colnames(DFR4()))
})
}发布于 2019-09-16 18:02:29
主要的解决方案是,在DFR3中,应该使用input$hot而不是DFR2()
DFR3 <- reactive({
req(input$hot)
hot_to_r(input$hot)
})使用%in%运算符,因为hot_to_r返回使用==运算符无法与普通因子进行比较的有序因子。(或者将DF2$agency_postcode定义为有序因子)
DFR4 <- reactive({
req(DFR3())
DF2[ which(DF2[,1] %in% DFR3()[1, 1]), ]
})输出:

https://stackoverflow.com/questions/57923287
复制相似问题