show the sum of specific columns based on rhandsontable values

删除回忆录丶 提交于 2019-12-11 15:58:38

问题


I am trying to create a shiny app, that would show a sum of a column (say mtcars$mpg) when rows are selected by the users. e.g if the first two boxes are clicked in rhandsontable, then below i should see a sum of 21 and 21. I am unable to wrap my head around it, and have made this code so far:

 library(shiny)
library(rhandsontable)

ui=fluidPage(
  rHandsontableOutput('table'),
  textOutput ('selected')
)

server=function(input,output,session)({

  df <- data.frame(head(transform(mtcars,  Selected = as.logical(NA)  )))

  output$table=renderRHandsontable(
    rhandsontable(df,selectCallback = TRUE,readOnly = FALSE)
  )
  output$selected<-renderText({

  })
}) # end server
shinyApp(ui = ui, server = server)

is there any way to achieve this ?


回答1:


I found a way ! saving the rhandsontable as an r object first and then applying subset & aggregate function , then rendering the result as a table :

i can use reactive like this

  tab1 <- reactive({
  if(!is.null(input$table )) {
    nt <- hot_to_r(input$table)
    nt.1<- subset(nt, Selected == TRUE, select = c(mpg,gear))
    nt.2 <- aggregate(nt.1$mpg ~ nt.1$gear , data = nt.1 , FUN = 'sum')  }
  })

:-)



来源:https://stackoverflow.com/questions/52989315/show-the-sum-of-specific-columns-based-on-rhandsontable-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!