问题
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