3

I have a rHandsontable table (rhandsontable package) and now I am searching a way how to transfer data from the table into a dataframe.
So far I have not found a clear clue how to perfrom this action.
I would be very grateful for the plain and clear example or useful link.

Besides, I have scanned a useful link. It throws a glimpse of light concerning how to manipulate data inside rhandsontable object.

But no explanation about methods used have been found. It looks like a black box testing. It would be nice if you could share some knowledge of this matter (if any).

Community
  • 1
  • 1
Dimon D.
  • 438
  • 5
  • 23

3 Answers3

2

Look into the shinysky package, as it uses Handsontable, which has hot.to.df function that would allow you to convert your datatable into dataframe. Below is a minimal example showing what I mean

rm(list = ls())
library(shiny)
library(shinysky)

server <- shinyServer(function(input, output, session) {

  # Initiate your table
  previous <- reactive({head(mtcars)})

  Trigger_orders <- reactive({
    if(is.null(input$hotable1)){return(previous())}
    else if(!identical(previous(),input$hotable1)){
      # hot.to.df function will convert your updated table into the dataframe
      as.data.frame(hot.to.df(input$hotable1))
    }
  })
  output$hotable1 <- renderHotable({Trigger_orders()}, readOnly = F)
  # You can see the changes you made
  output$tbl = DT::renderDataTable(Trigger_orders())
})

ui <- basicPage(mainPanel(column(6,hotable("hotable1")),column(6,DT::dataTableOutput('tbl'))))
shinyApp(ui, server)
Pork Chop
  • 28,528
  • 5
  • 63
  • 77
  • I have used such a script (abstract): print(hot.to.df(input$all_updates$data)). The outcome is null, the object 'all_updates' is rhandsontable item. Is 'hot.to.df' applicable to rhandsontable? – Dimon D. Jul 13 '16 at 11:17
  • 1
    I have found the solution - the function 'hot_to_r' from rhandsontable package. It converts into dataframe. You can check my answer. – Dimon D. Jul 13 '16 at 12:36
1

Well, I have found the way to convert rhandsontable object into dataframe in R.

It seems to be very easy with the function 'hot_to_r' but the overall function description is poor.

So look up some examples and explanations outside the package description (pdf) on CRAN. In my case I have used black-box testing.

Here is my case:

test_case <- hot_to_r(input$all_updates)

The variable 'test_case' is a dataframe.

Dimon D.
  • 438
  • 5
  • 23
  • Where does this code go? Is there a minimum working example you could post? – Matt Nov 03 '16 at 01:33
  • @Matt what exactly would you like to see? Here is my working script from server side: zzz <- hot_to_r(input$all_updates); print(zzz); predictor_vector <- rep(predictors,nrow(zzz)); zzz <-cbind(predictor_vector,zzz) ; colnames(zzz)[1] <- 'predictor'; – Dimon D. Nov 03 '16 at 12:33
0

I have created a reproducible example here that I used for my existing shiny apps:

library(shiny)
library(rhandsontable)

server <- shinyServer(function(input, output, session) {
  
  # 1. I assign mtcar df to my rhandsontable ("test_table")
  output$test_table <- renderRHandsontable({
    rhandsontable(mtcars)
  })
  
  # 2. I can make some changes to this rhandsontable and generate the resulting table as dataframe
  values <- reactiveValues(data = NULL)
  
  observe({
    values$data <- hot_to_r(input$test_table)
  })

  # 3. I assign this df to a variable call df1
  df1 <- reactive({
    values$data
  })
  
  # 4. Click on action button to see df
  observeEvent(input$print_df_btn, {
    print(df1())
  })
  
})

ui <- basicPage(
  mainPanel(
    rHandsontableOutput("test_table"),
    br(),
    actionButton("print_df_btn", "Print dataframe to console")
    )
  )

shinyApp(ui, server)
Dwight
  • 124
  • 2
  • 12