【问题标题】:show the sum of specific columns based on rhandsontable values根据 rhandsontable 值显示特定列的总和
【发布时间】:2019-03-30 01:38:34
【问题描述】:

我正在尝试创建一个闪亮的应用程序,当用户选择行时,它将显示一列的总和(例如 mtcars$mpg)。例如,如果在 rhandsontable 中单击了前两个框,那么下面我应该会看到 21 和 21 的总和。我无法理解它,并且到目前为止已经编写了此代码:

 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)

有什么方法可以实现吗?

【问题讨论】:

    标签: r shiny rhandsontable


    【解决方案1】:

    我找到了方法!首先将 rhandsontable 保存为 r 对象,然后应用子集和聚合函数,然后将结果呈现为表格:

    我可以像这样使用响应式

      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')  }
      })
    

    :-)

    【讨论】:

      猜你喜欢
      • 2017-12-02
      • 2017-01-10
      • 2017-01-08
      • 1970-01-01
      • 2019-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多