2016-05-18 62 views
1

我最近在r中發現了rhandsontable包,它將在我的一些r閃亮項目中非常有用。我稍微修改了我在這裏看到的Get selected rows of Rhandsontable作爲我將使用此包的一個小測試器。 我希望能夠讓用戶使用rhandsontable包更改r內的數據幀的值。因此,我希望每次更改該值時都要更新df [1,1]。當我們圍繞渲染函數,特別是renderRHandsontable函數封裝一個反應函數時,我感到有點困惑。我用繪圖功能使用了反應函數,但這有些不同。反應性和rhandsontable

library(shiny) 
library(rhandsontable) 

ui=fluidPage(
    rHandsontableOutput('table'), 
    verbatimTextOutput('selected'), 
    verbatimTextOutput("tr") 
) 
server=function(input,output,session)({ 

a<-c(1,2) 
b<-c(3,4) 
c<-rbind(df1,df2) 
df1<-data.frame(df3) 

#need reactive function around the following 

    output$table=renderRHandsontable(
    rhandsontable(df1,selectCallback = TRUE,readOnly = FALSE) 
) 
    output$selected=renderPrint({ 
    cat('Selected Row:',input$table_select$select$r) 
    cat('\nSelected Column:',input$table_select$select$c) 
    cat('\nSelected Cell Value:',input$table_select$data[[input$table_select$select$r]][[input$table_select$select$c]]) 
    df1[input$table_select$select$r,input$table_select$select$c]<-input$table_select$data[[input$table_select$select$r]][[input$table_select$select$c]] 
    }) 
#need reactive function around the following 
    output$tr <- renderText({ 
df1[1,1] 
}) 

}) 
# end server 
shinyApp(ui = ui, server = server) 

這是一個有趣的領域,將在我閃亮的應用中打開很多,供用戶隨意使用。

謝謝

+0

我相信我需要df1和renderRHandsontable都是反應性操作。只是不確定正確的做法。 – mike

+0

http://stackoverflow.com/questions/33722757/update-handsontable-by-editing-table-and-or-eventreactive -----非常有幫助 – mike

回答

0

您的編碼是不可複製的。在您的服務器功能開始時,您在df1df2上使用rbind(),這兩個對象都不存在。 R將拋出一個錯誤(應該!)

因爲我必須假設你的數據幀實際上如下:

a<-c(1,2) 
    b<-c(3,4) 
    c<-rbind(a,b) 
    df1<-data.frame(c) 

從Rhandsontable輸出反應綁定到textOutput ,您可以使用Shiny的observe()功能或更好的功能hot_to_r功能rhandsontable本身。該函數將可交換數據轉換爲R對象。

不改變你的ui功能,這將是你server功能:

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

    a<-c(1,2) 
    b<-c(3,4) 
    c<-rbind(a,b) 
    df1<-data.frame(c) 

output$table<-renderRHandsontable(
     rhandsontable(df1) 
) 

#use hot_to_r to glue 'transform' the rhandsontable input into an R object 
output$tr <- renderText({ 
     hot_to_r(input$table)[1,1] 
}) 

}) 

然後繼續打電話給你閃亮的應用程序像往常一樣:shinyApp(ui = ui, server = server)和你output$tr現在反應你的桌子上的任何修改。