在闪亮的应用程序中保留rhandsontable的行顺序

时间:2016-03-23 10:59:14

标签: r shiny

我正在运行here的示例。

library(rhandsontable)
library(shiny)

runApp(shinyApp(
  ui = fluidPage(rHandsontableOutput("hot")),
  server = function(input, output, session) {
    fname <- "mtcars2.csv"
    values <- reactiveValues()
    setHot <- function(x) values[["hot"]] = x

    observe({
      if(!is.null(values[["hot"]])) write.csv(values[["hot"]], fname)
    })

    output$hot <- renderRHandsontable({
      if (!is.null(input$hot)) {
        DF <- hot_to_r(input$hot)
      } else {
        DF <- read.csv("mtcars.csv", stringsAsFactors = FALSE)
      }
      setHot(DF)
      rhandsontable(DF) %>%
        hot_table(highlightCol = TRUE, highlightRow = TRUE) %>%
        hot_cols(columnSorting = TRUE)
    })
  }
))

我希望对表格所做的更改保存在文件mtcars2.csv中。我还想保留行顺序。在project home page中,它表示“排序仅影响窗口小部件,不会重新排序原始数据集”。我可以以某种方式获取表的当前视图并保存它吗?

2 个答案:

答案 0 :(得分:1)

回答此问题的最佳方法是在https://github.com/jrowen/rhandsontable提交问题。目前,这些lines仅定义了handsontable个事件的部分列表。此列表不包含afterColumnSort,这将是您所需要的。这是一个快速的黑客,可以部分回答你的问题。

library(rhandsontable)
library(shiny)
library(htmlwidgets)

runApp(shinyApp(
  ui = fluidPage(
    rHandsontableOutput("hot"),
    tags$script(
'
setTimeout(
  function() {
    HTMLWidgets.find("#hot").hot.addHook(
      "afterColumnSort",
      function(){
        console.log("sort",this);
        Shiny.onInputChange(
          "hot_sort",
          {
            data: this.getData()
          }
        )
      }
    )
  },
  1000
)
' 

    )
  ),
  server = function(input, output, session) {
    observeEvent(
      input$hot_sort
      ,{
        print(input$hot_sort$data)
      }
    )

    output$hot <- renderRHandsontable({
      if (!is.null(input$hot)) {
        DF <- hot_to_r(input$hot)
      } else {
        DF <- mtcars
      }
      rhandsontable(DF) %>%
        hot_table(highlightCol = TRUE, highlightRow = TRUE) %>%
        hot_cols(columnSorting = TRUE)
    })
  }
))

答案 1 :(得分:0)

我认为有一种方法可以保留DataTables中已排序的列,以保持闪亮,悲伤!

使用以下代码,我可以将闪亮应用中的更改保存到文件mtcars2.csv。有趣!按所需列进行排序,单击任何数据单元格并按Enter键将行顺序保存到mtcars2.csv。同意timelyportolio关于在git上提交问题的观点。

R代码:

library(shiny)
library(rhandsontable)

runApp(shinyApp(
  ui = fluidPage(titlePanel("Edit Data File"),
                 helpText("Changes to the table will be automatically saved to the source file."),
             # actionButton("saveBtn", "Save"),
             rHandsontableOutput("hot")),
  shinyServer(function(input, output, session) {
  values = reactiveValues()
  data = reactive({
    if (is.null(input$hot)) {
      hot = read.csv("mtcars.csv", stringsAsFactors = FALSE)
     } else {
      hot = hot_to_r(input$hot)
    }

    # this would be used as a function input
    values[["hot"]] = hot
    hot
  })

  observe({
    # input$saveBtn
    if (!is.null(values[["hot"]])) {
      write.csv(values[["hot"]], "mtcars.csv", row.names = FALSE)
    }
  })

  output$hot <- renderRHandsontable({
    hot = data()
    if (!is.null(hot)) {
      hot = rhandsontable(hot) %>%
        hot_table(highlightCol = TRUE, highlightRow = TRUE) %>%
        hot_cols(columnSorting = TRUE)
      hot
    }
  })
})
))