我想在一个闪亮的应用程序中显示和编辑一个rhandsontable。由于我的数据框架相当大,所以我希望用户能够过滤特定的行,而不是显示整个1000行(参见下面的示例)。我可以基于hot为子集input$row创建一个反应性值,但是只有DF[input$row,]被分配给input$hot,因此,下次当我得到input$hot的值时,它返回一个只有一行的数据帧。
library(shiny)
library(rhandsontable)
ui <- shinyUI(fluidPage(
numericInput("rows","row to filter",value = 1),
rHandsontableOutput("hot")
))
server <- shinyServer(function(input, output, session) {
# render handsontable output
output$hot <- renderRHandsontable({
if (!is.null(input$hot)) {
DF <- hot_to_r(input$hot)
} else {
set.seed(42)
DF <- data.frame(a=1:1000, b= rnorm(1000))
}
rhandsontable(DF)
})
})
runApp(list(ui=ui, server=server))是否有一个过滤参数可以应用于rhandsontable(),允许我在不实际设置数据帧的情况下呈现经过过滤的数据帧版本,这样链接的input$hot就不会受到影响(当然,用户所做的任何编辑除外)?
我希望用户编写行,以便在textInput框row中筛选,然后相应地过滤表。至关重要的是,nrow(hot_to_r(input$hot)) == 1000仍然是正确的:

发布于 2016-12-17 11:55:05
您不能这样做,使用过滤器,但您可以缓存一行,并在事情发生变化时将数据放回。
这是一种让人发亮的方法。比我想象的要好得多,我尝试了很多其他的方法,但这些方法并没有奏效,所以对我来说也是一次学习的经历。
library(shiny)
library(rhandsontable)
set.seed(1234)
# Data and a couple utility functions
nrow <- 1000
DF <- data.frame(a = 1:nrow,b = abs(rnorm(nrow)))
lastrow <- 1
getrowfromDF <- function(idx) {
return(DF[idx,])
}
putrowintoDF <- function(rowdf,idx) {
for (ic in 1:ncol(DF)) {
DF[idx,ic] <<- rowdf[1,ic]
}
}
u <- shinyUI(fluidPage(
numericInput("row","row to filter",value = lastrow,min = 1,max = nrow(DF)),
verbatimTextOutput("rowstat"),
rHandsontableOutput("hot")
))
s <- shinyServer(function(input,output,session) {
# reactive row status
rs <- reactiveValues()
rs$lstrow <- rs$currow <- 1
# record changes from user editing here
observeEvent(input$hot, {
if (!is.null(input$hot)) {
ndf <- data.frame(input$hot$data) # convert from list
#putrowintoDF(ndf,rs$currow) # original - has inconsistency issue when
# you change rows without closing edit
# with enter key in rhandsontable grid input$hot
putrowintoDF(ndf,ndf[1,1]) # new, consistent, but relies on editable data for state
}
})
# slide the row to the new position here
observeEvent(input$row, {
rs$lstrow <<- rs$currow
rs$currow <<- input$row
})
# render handsontable output
output$hot <- renderRHandsontable({
ndf <- getrowfromDF(rs$currow)
rhandsontable(ndf)
})
# just for debug
output$rowstat <- renderPrint({ sprintf("rowstat: cur:%d last:%d",rs$currow,rs$lstrow) })
})
shinyApp(ui = u,server = s)我本来想要一个没有全局变量分配和纯反应的解决方案,而不是观察,但我认为这是不可能的。
更新
我提出的原稿有一个一致性错误,我错过了,因为我使用的是一个版本的闪亮,没有数字控制增量箭头。当您用数字控件input$row更改行时发生了这种情况,而没有用enter键关闭rhandsontable input$hot中的编辑或更改焦点,并导致在dataframe DF中更新错误的行。
修复是使用input$hot中的数据来维护这种状态,但这可能是危险的,因为用户可以编辑它。或者这是一个特征..。
无论如何,这里有一个屏幕截图,但是您确实必须使用这些值来确保它的工作,并且没有bug:

发布于 2019-04-29 13:15:41
您可以通过有条件地隐藏行:https://github.com/jrowen/rhandsontable/issues/219#issuecomment-487574898来做到这一点。
https://stackoverflow.com/questions/41161822
复制相似问题