首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在闪亮的应用程序中保持rhandsontable的行顺序

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

Stack Overflow用户
提问于 2016-03-23 10:59:14
回答 2查看 884关注 0票数 5

我正在运行一个来自这里的示例。

代码语言:javascript
复制
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中。我还想保留行顺序。在项目主页中,它说“排序只会影响小部件,不会重新排序原始数据集”。我可以以某种方式获取当前的桌子视图并保存它吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-03-31 05:28:49

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

代码语言:javascript
复制
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)
    })
  }
))
票数 2
EN

Stack Overflow用户

发布于 2016-03-31 13:15:49

我认为没有办法将DataTables中的排序列保存为闪亮、悲伤的列!

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

R代码:

代码语言:javascript
复制
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
    }
  })
})
))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36176298

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档