【问题标题】:reactivity and rhandsontable反应性和rhandsontable
【发布时间】:2016-09-14 10:34:23
【问题描述】:

我最近在 r 中发现了 rhandsontable 包,它将在我的一些 r 闪亮项目中非常有用。我稍微修改了我在这里看到的Get selected rows of Rhandsontable 作为我将使用这个包的小测试器。 我希望能够让用户使用 rhandsontable 包从 r 中更改数据框的值。所以在这里我希望 df[1,1] 在每次更改该值时更新。当涉及到围绕渲染函数特别是 renderRHandsontable 函数包装反应函数时,我有点困惑。我在绘图中使用了反应函数,但这有点不同。

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)

这是一个有趣的领域,它将在我闪亮的应用程序中打开很多供用户玩耍的地方。

谢谢

【问题讨论】:

标签: r shiny handsontable


【解决方案1】:

您在此处的代码不可重现。在您的服务器功能开始时,您在df1df2 上使用了rbind(),而这两个对象都不存在。 R 会抛出一个错误(它应该!)

因此,我将不得不假设您的数据框实际上如下:

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

要将 Rhandsontable 的反应性输出绑定到您的 textOutput,您可以使用 Shiny 的 observe() 函数,甚至更好的是 rhandsontable 本身的方便的 hot_to_r 函数。该函数将handsontable数据转换为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]
})

})

然后像往常一样继续调用您的 Shiny 应用程序:shinyApp(ui = ui, server = server) 和您的 output$tr 现在会对您桌面上的任何编辑做出反应。

【讨论】:

    猜你喜欢
    • 2020-12-01
    • 2019-04-10
    • 2020-01-01
    • 2017-06-28
    • 2019-07-19
    • 1970-01-01
    • 2017-10-19
    • 2018-03-28
    • 2015-11-24
    相关资源
    最近更新 更多