我稍微修改了一下我在这里看到的: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)
字符串
1条答案
按热度按时间quhf5bfb1#
这里的代码是不可复制的。在服务器函数的开始,当
df1
和df2
都不存在时,您在df1
和df2
上使用了rbind()
。R将抛出错误(它应该!)因此,我必须假设您的 Dataframe 实际上是以下内容:
字符串
要将来自Rhandsontable的React性输出绑定到
textOutput
,可以使用来自Shiny的observe()
函数,或者更好的,来自rhandsontable
本身的方便的hot_to_r
函数。该函数将handsontable数据转换为R对象。在不改变
ui
函数的情况下,这将是您的server
函数:型
然后继续像往常一样调用您的Shiny应用程序:
shinyApp(ui = ui, server = server)
,您的output$tr
现在会对表上的任何编辑做出React。