简体   繁体   中英

Convert data from rhandsontable object into dataframe in Shiny

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).

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)

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM