问题描述
我正在尝试创建一个闪亮的应用程序,当用户选择行时,该应用程序将显示一列的总和(例如mtcars $ mpg).例如,如果在rhandsontable中单击了前两个框,那么在下面我应该看到21和21的总和.我无法绕过它,并且到目前为止已经编写了以下代码:
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 ?
推荐答案
我找到了一种方法!首先将rhansontable保存为r对象,然后再应用子集&聚合函数,然后将结果呈现为表:
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') }
})
:-)
这篇关于显示基于rhansontable值的特定列的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!