2016-09-21 134 views
2

我使用(awesome)包rhandsontable,稍後將包含在閃亮的網頁中。用戶可以在某些地方點擊,我想知道如何檢索點擊哪些行的信息。 下面是一個例子,(爲處於ř終端複製&糊):從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) 

我考慮它轉換爲ř對象:

hot_to_r(r) 

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

回答

0

看一看shinysky包。請注意,我用展示所實現的改變也表,以便您可以檢查你的工作

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) 

enter image description here

+0

感謝您的詳細示例。所以你創建一個DF,然後創建一個表,然後檢查它是否已經改變,並創建一個DF,對吧?關於我最初的問題,我現在可以通過執行來檢索已更改的行:「MyChanges [which(MyChanges $ stop.speed == TRUE),]」,是否正確? –

+0

看起來像ShinySky包不適合最新的R版本 包'shinysky'不可用(對於R版本3.3.1) 對於R 3.2.4同樣的錯誤 –

+0

我在R版本3.1.3和' shinysky版本0.1.2 –

0

此外shinysky,另一種解決方案是回撥handsontable:

server.R

DF = hot_to_r(input$table) 

在ui.R中,表格已被調用使用:

rHandsontableOutput("table") 

DF然後可以用作任何R數據幀

0

可以通過以下下面提到的步驟安裝在r中3.3.1 shinysky包: - 一旦在

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

0

閃亮的應用程序,你可以使用:

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 

你可以寫一個小函數'翻譯'的行和列號到你的dataframe/matrix/etc中的一個位置,或者像上面那樣訪問這個值。

希望這會有所幫助。