首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过修改另一个rhandsontable来修改rhandsontable

通过修改另一个rhandsontable来修改rhandsontable
EN

Stack Overflow用户
提问于 2019-09-13 20:01:37
回答 1查看 110关注 0票数 0

我有下面的数据框架:

代码语言:javascript
复制
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的第一行,它应该显示修改后的数据集,但它没有。

代码语言:javascript
复制
#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()))   


   })



}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-16 18:02:29

主要的解决方案是,在DFR3中,应该使用input$hot而不是DFR2()

代码语言:javascript
复制
DFR3 <- reactive({
  req(input$hot)
  hot_to_r(input$hot)
})

使用%in%运算符,因为hot_to_r返回使用==运算符无法与普通因子进行比较的有序因子。(或者将DF2$agency_postcode定义为有序因子)

代码语言:javascript
复制
DFR4 <- reactive({
  req(DFR3())
  DF2[ which(DF2[,1] %in% DFR3()[1, 1]), ]
})

输出:

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57923287

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档