How can I reset the selected cell range each time a new dataset is generated in the example below?
library(shiny)
library(data.table)
library(rhandsontable)
shinyApp(
ui = fluidPage(
actionButton('gendata', 'Generate data'),
hr(),
verbatimTextOutput('verb'),
hr(),
rHandsontableOutput('table_original'),
hr(),
rHandsontableOutput('table_subset')
),
server = function(input, output, session) {
mydata <- reactive({
req(input$gendata)
n_dim <- sample(2:8, 1)
data.table( matrix(rnorm(n_dim**2), n_dim, n_dim) )
})
output$table_original <- renderRHandsontable({
req(mydata())
rhandsontable(data = mydata(), selectCallback = TRUE)
})
output$table_subset <- renderRHandsontable({
req(input$table_original_select$select)
selected_rows <- input$table_original_select$select$rAll
selected_cols <- input$table_original_select$select$cAll
DT <- hot_to_r(input$table_original)
DT <- DT[selected_rows, ..selected_cols] # subset
rhandsontable(DT)
})
output$verb <- renderPrint({
str(input$table_original_select$select)
})
}
)
Edit 1
I think a solution might be to use javascript, but I am not familiar enought with this language to figure it out. See this post.
Edit 2
I managed to avoid the error that was occuring when a new dataset was generated and the previous range was larger than the current dataset. I still hope that there can be a better solution.
library(shiny)
library(data.table)
library(rhandsontable)
shinyApp(
ui = fluidPage(
actionButton('gendata', 'Generate data'),
hr(),
verbatimTextOutput('verb'),
hr(),
rHandsontableOutput('table_original'),
hr(),
rHandsontableOutput('table_subset')
),
server = function(input, output, session) {
first_range_selection <<- TRUE
mydata <- reactive({
req(input$gendata)
n_dim <- sample(2:8, 1)
data.table( matrix(rnorm(n_dim**2), n_dim, n_dim) )
})
observeEvent(mydata(), {
if (first_range_selection) {
reset_range <<- FALSE
first_range_selection <<- FALSE
} else {
reset_range <<- TRUE
}
})
output$table_original <- renderRHandsontable({
req(mydata())
rhandsontable(data = mydata(), selectCallback = TRUE)
})
output$table_subset <- renderRHandsontable({
req(input$table_original_select$select)
if (reset_range) {
reset_range <<- FALSE
return()
}
selected_rows <- input$table_original_select$select$rAll
selected_cols <- input$table_original_select$select$cAll
DT <- hot_to_r(input$table_original)
DT <- DT[selected_rows, ..selected_cols] # subset
rhandsontable(DT)
})
output$verb <- renderPrint({
str(input$table_original_select$select)
})
}
)
You can program two observeEvent
s:
output$table_subset
to NULL at input$gendata
output$table_subset
at input$table_original_select
This way it works fine on my side:
library(shiny)
library(data.table)
library(rhandsontable)
shinyApp(
ui = fluidPage(
actionButton('gendata', 'Generate data'),
hr(),
verbatimTextOutput('verb'),
hr(),
rHandsontableOutput('table_original'),
hr(),
rHandsontableOutput('table_subset')
),
server = function(input, output, session) {
mydata <- reactive({
req(input$gendata)
n_dim <- sample(2:8, 1)
data.table( matrix(rnorm(n_dim**2), n_dim, n_dim) )
})
output$table_original <- renderRHandsontable({
req(mydata())
rhandsontable(data = mydata(), selectCallback = TRUE)
})
observeEvent(input$gendata, {
output$table_subset <- NULL
})
observeEvent(input$table_original_select, {
output$table_subset <- renderRHandsontable({
req(input$table_original_select$select)
selected_rows <- input$table_original_select$select$rAll
selected_cols <- input$table_original_select$select$cAll
DT <- hot_to_r(input$table_original)
DT <- DT[selected_rows, ..selected_cols] # subset
rhandsontable(DT)
})
})
output$verb <- renderPrint({
str(input$table_original_select$select)
})
}
)