React性和可控性

jgwigjjp  于 5个月前  发布在  React
关注(0)|答案(1)|浏览(36)

我稍微修改了一下我在这里看到的:Get selected rows of Rhandsontable作为我将使用rhandsontable的一个小测试。我希望能够让用户使用rhandsontable包从r中更改 Dataframe 的值。所以在这里,我希望df[1,1]在每次更改该值时都进行更新。当涉及到在渲染函数周围 Package React函数时,我有点困惑特别是renderRHandsontable函数。我在绘图时使用过React函数,但这有点不同。

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)

字符串

quhf5bfb

quhf5bfb1#

这里的代码是不可复制的。在服务器函数的开始,当df1df2都不存在时,您在df1df2上使用了rbind()。R将抛出错误(它应该!)
因此,我必须假设您的 Dataframe 实际上是以下内容:

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

字符串
要将来自Rhandsontable的React性输出绑定到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现在会对表上的任何编辑做出React。

相关问题