Keep the line order in your hand in a brilliant app

I am running an example from 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) }) } )) 

I want the changes made to the table saved in the mtcars2.csv file. I also want to keep the order of the lines. The project’s homepage says that "sorting only affects the widget and does not change the order of the original dataset." Is there any way to get the current view of the table and save it?

+5
source share
2 answers

The best way to answer this question is to submit a question at https://github.com/jrowen/rhandsontable . Currently, these lines define only a partial list of handsontable events. This list does not include the afterColumnSort that you need. Here's a quick hack to partially answer your question.

 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
source

I don't think there is a way to save the sorted columns in DataTables for brilliant, Sad!

With the code below, I can save the changes made in a brilliant application to the mtcars2.csv file. Interesting! sorting by the desired column, clicking on any data cell and pressing the enter key saves the row order in mtcars2.csv . Accept the timelyportolio tag when submitting a problem to git.

R Code:

 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
source

Source: https://habr.com/ru/post/1245620/


All Articles