首页 > 解决方案 > `rhandsontable` 包中的 `hot_to_r()` 函数在“正常”.R 脚本中不起作用 - 如何检查闪亮数据的方面?

问题描述

我试图了解如何hot_to_r()转换使用rhandsontable(). 我的目标是查看您在hot_to_r()处理后获得的对象,以便调试闪亮的应用程序。

但是,您似乎无法在闪亮的应用程序之外执行此操作,在 .R 脚本内。

[编辑] 进一步搜索将我引向这篇文章。我们确定您获得的对象与来自的对象fromJSON()相同吗hot_to_r()

这是我试图用来查看输出的 .R 脚本hot_to_r()

library(rhandsontable)
library(tidyverse)
library(jsonlite)

# dummy dataframe
df = data.frame(id = c("a", "b"),
                val = c(0.75, 0.25))

# convert it to a "rhansontable" object
test = rhandsontable(df)

# try to convert it back to a dataframe but it doesn't work
test_hot = hot_to_r(test)

# however, this works but I am not sure if test_json is the same as test_hot
test_json = fromJSON(test$x$data)

test_hot = hot_to_r(test)导致此错误:

test_df = hot_to_r(测试)

(function (data, changes, params, ...) 中的错误:缺少参数“params”,没有默认值

我对闪亮很陌生;我错过了什么吗?

您无法hot_to_r()在 .R 脚本中工作是否正常?

如果是,您如何检查闪亮应用程序中数据的“方面”?总体目标是使用由用户填充的 rhandsontable 进行计算。我想将此对象转换为数据框,以便进行正确的修改以获得“整洁”的数据集。

标签: rshinyrhandsontable

解决方案


hot_to_r旨在与闪亮的输入(包含params错误消息中提到的参数)一起使用。

为了在闪亮的上下文中进行调试,我建议使用browser().

请检查以下内容:

library(rhandsontable)
library(shiny)

ui <- fluidPage(
  rHandsontableOutput("myTable")
)

server <- function(input, output, session) {
  # dummy dataframe
  df = data.frame(id = c("a", "b"),
                  val = c(0.75, 0.25), stringsAsFactors = FALSE)

  # convert it to a "rhansontable" object
  output$myTable <- renderRHandsontable({rhandsontable(df)})

  observeEvent(input$myTable, {
    test_df = hot_to_r(input$myTable)
    print(test_df)
    # browser() # uncomment for debugging
  })

}

shinyApp(ui, server)

但是,fromJSON也是一种有效的方法。


推荐阅读