【问题标题】:Convert data from rhandsontable object into dataframe in Shiny在 Shiny 中将数据从 rhandsontable 对象转换为数据框
【发布时间】:2016-11-14 09:45:31
【问题描述】:

我有一个 rHandsontable 表(rhandsontable 包),现在我正在寻找如何将数据从表传输到数据框的方法。
到目前为止,我还没有找到如何执行此操作的明确线索。
我将非常感谢简单明了的示例或有用的链接。

此外,我扫描了一个有用的link。它揭示了如何在 rhandsontable 对象中操作数据。

但是没有找到关于使用方法的解释。它看起来像一个黑盒测试。如果您能分享一些这方面的知识(如果有的话),那就太好了。

【问题讨论】:

    标签: r shiny handsontable rhandsontable


    【解决方案1】:

    查看shinyskypackage,因为它使用Handsontable,它具有hot.to.df 函数,可让您将数据表转换为dataframe。下面是一个显示我的意思的最小示例

    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)
    

    【讨论】:

    • 我使用过这样的脚本(摘要):print(hot.to.df(input$all_updates$data))。结果为空,对象 'all_updates' 是 rhandsontable 项。 'hot.to.df' 是否适用于 rhandsontable?
    • 我找到了解决方案 - rhandsontable 包中的函数 'hot_to_r'。它转换为数据框。你可以看看我的回答。
    【解决方案2】:

    好吧,我已经找到了在 R 中将 rhandsontable 对象转换为数据框的方法。

    使用函数“hot_to_r”似乎很容易,但整体函数描述

    因此,请在 CRAN 上的 package description (pdf) 之外查找一些示例和解释。就我而言,我使用了黑盒测试。

    这是我的情况:

    test_case <- hot_to_r(input$all_updates)
    

    变量“test_case”是一个数据框。

    【讨论】:

    • 这段代码去哪儿了?您可以发布一个最低限度的工作示例吗?
    • @Matt 你到底想看什么?这是我在服务器端的工作脚本: zzz
    【解决方案3】:

    我在这里创建了一个可重现的示例,用于我现有的闪亮应用:

    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)
    

    【讨论】:

      猜你喜欢
      • 2020-06-12
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 2020-03-27
      • 1970-01-01
      • 2021-11-18
      • 2022-01-15
      • 1970-01-01
      相关资源
      最近更新 更多