Rhandsontable中的“反应/计算”列,其中源数据是来自反应值的数据帧

时间:2018-10-05 14:39:13

标签: r shiny rhandsontable

我有一个闪亮的应用程序,该应用程序将根据用户想要查看的产品类别显示一个rhansontable。如果用户在另一列中输入了不同的值,我需要重新计算其中一列,并且我发现此示例很有用: Reactive/Calculate Columns in rhandsontable in shiny- rstudio 除了我从反应性对象获取表的数据之外,因此当我将“ datacopy”重定向到该函数以获取数据时,此代码不起作用。有人可以帮我实现吗? 这是示例代码:

library(shiny)
library(datasets)
library(rhandsontable)
library(data.table)

ui=fluidPage(
  rHandsontableOutput("table1")
)

server=function(input, output, session) {

 mt=reactive({
 datacopy <- NULL
 #For initial data upload
 if(is.null(input$table1)){
     datacopy= mtcars[, names(mtcars) %in% c("mpg" , "cyl" , "disp")]
     datacopy=data.table(datacopy) 
 }else{
  datacopy = hot_to_r(input$table1)
  #If there is change in data
  if(!is.null(input$table1$changes$changes)){
    row.no <- unlist(input$table1$changes$changes)[1]
    col.no <- unlist(input$table1$changes$changes)[2]
    new.val <- unlist(input$table1$changes$changes)[4]
    #If the changed value is mpg or cyl
    if(col.no == 0 || col.no == 1){
      datacopy[(row.no+1), 3] = datacopy[(row.no+1), 1]*datacopy[(row.no+1), 2]
    }else{
      datacopy[(row.no+1), 2] = datacopy[(row.no+1), 1]*datacopy[(row.no+1), 3]
    }
  }
}
datacopy
  })  

output$table1=renderRHandsontable({
    rhandsontable(mt())
})
}
shinyApp(ui, server)

0 个答案:

没有答案