从 rhandsontable 对象(R,R shiny)中检索值

Retrieving values from an rhandsontable object (R, R shiny)

我使用(很棒的)包 rhandsontable,稍后将包含在 R 闪亮的网页中。用户可以在某些地方点击,我想知道如何检索点击了哪些行的信息。 这是一个示例,(在 R 终端中复制和粘贴):

library(rhandsontable)

## Create the dataset
min = c(1,seq(2,34,by=2))
kmh = c(0,seq(7,23,by=1))
mph = round( kmh / 1.609344, digits=0 )
stop.speed = rep(FALSE, length(min))    
DF = data.frame(min, kmh, mph, stop.speed, stringsAsFactors = FALSE)

#plot the table
r = rhandsontable(DF, useTypes = TRUE)

我考虑过将其转换为 R 对象:

hot_to_r(r)

Error in (function (data, changes, params, ...)  : 
argument "params" is missing, with no default

看看 shinysky 包。请注意,我还显示了 table 以及已实施的更改,因此您可以检查您的工作

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

## Create the dataset
min = c(1,seq(2,34,by=2))
kmh = c(0,seq(7,23,by=1))
mph = round( kmh / 1.609344, digits=0 )
stop.speed = rep(FALSE, length(min))    
DF = data.frame(min, kmh, mph, stop.speed, stringsAsFactors = FALSE)

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

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

  MyChanges <- 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({MyChanges()}, readOnly = F)
  # You can see the changes you made
  output$tbl = DT::renderDataTable(MyChanges())
})

ui <- basicPage(mainPanel(column(6,hotable("hotable1")),column(6,DT::dataTableOutput('tbl'))))
shinyApp(ui, server)

除了shinysky,还有一个办法就是回调handsontable:

server.R

DF = hot_to_r(input$table)

在 ui.R 中,table 被调用使用:

rHandsontableOutput("table")

然后 DF 可以用作任何 R 数据框

您可以按照以下步骤在 r 3.3.1 中安装 shinysky 包:-

install.packages("devtools") 
devtools::install_github("AnalytixWare/ShinySky")

进入 shiny 应用程序后,您可以使用:

input$table_select$select$r # access the row number
input$table_select$select$c # access the column number
input$table_select$data[[input$table_select$select$r]][[input$table_select$select$c]] # access the data in a cell

您可以编写一个小函数 'translate' 将行号和列号放入 dataframe/matrix/etc 中的某个位置,或者像上面那样访问该值。

希望对您有所帮助。

这个问题已有 4 年历史,但仍然与 rhandsontable 软件包用户相关。此外,上面 Lyx 提供的解决方案不再有效。以下是该问题的简单解决方法。

每个rhandsontable对象都是一个深度嵌套的列表。它的元素之一是 data 元素,它本身嵌套在 x 元素下。但是,数据是 json 格式,但可以使用 jsonlite 包中的 fromJSON() 函数轻松将其转换为 data.frame

library(rhandsontable)
library(jsonlite)

hands_on_table <- rhandsontable(mtcars)
data_frame <- fromJSON(hands_on_table$x$data)
head(data_frame)

   mpg cyl disp  hp drat    wt  qsec vs am gear carb
1 21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
2 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
3 22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
4 21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
5 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
6 18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

编辑:

还需要指出的是,使用 hot_to_rjsonlite::fromJSON 的主要区别在于,前者在应用程序运行时使用 运行,而后者仅适用于交互式 R 会话。