2016-05-18 63 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现在反应你的桌子上的任何修改。