rhandson Get selected rows of Rhandsontable

Published by onesixx on

 

 selectCallback=TRUE을 사용

library(shiny)
library(rhandsontable)
library(data.table)

init_env4 <- function(){
    env <- new.env()
    env$select_idx <- NULL
    return(env)
}
env4 <- init_env4()

ui = fluidPage(
    # rHandsonTable Grid
    rHandsontableOutput('rht_table'), br(),
    
    # Display result 
    verbatimTextOutput('resultDisplay1'),
    verbatimTextOutput('resultDisplay2'),
    verbatimTextOutput('resultDisplay3')
)

server <- function(input, output, session) {
    # Data for grid
    baseData <- data.table(AA=c(1:10), BB=TRUE, CC=LETTERS[1:10], DD=c(11:20))
    
    # Define rhandsonTable
    output$rht_table <- renderRHandsontable(
        rhandsontable(baseData, selectCallback=T) %>%  
            hot_table(highlightRow=T, highlightCol=F) %>%   #stretchH="last"
            hot_cols(colWidths=c(80, 80, 100, 60), fixedColumnsLeft=1, columnSorting=T) %>%
             hot_col(col="AA", readOnly=T) %>%
             hot_col(col="BB", readOnly=F, type="checkbox") %>%
             hot_col(col="CC", readOnly=T) %>%
             hot_col(col="DD", readOnly=T) 
    )
    
    # Display Selected Row
    output$resultDisplay1 <- renderPrint({
        cat('Selected Row      : ', input$rht_table_select$select$r,'\n')
        cat('Selected Column   : ', input$rht_table_select$select$c,'\n')
        cat('Selected Cell Val : ', input$rht_table_select$data[[input$rht_table_select$select$r]][[input$rht_table_select$select$c]],'\n')
        cat('Selected Range    : ','(',input$rht_table_select$select$r, ',',input$rht_table_select$select$c, ')~(',
                                       input$rht_table_select$select$r2,',',input$rht_table_select$select$c2,')', sep="",'\n')
    })
    # Display Changed Row
    output$resultDisplay2 <- renderPrint({
        cat('Changed Cell Row Column : ', input$rht_table$changes$changes[[1]][[1]], input$rht_table$changes$changes[[1]][[2]],'\n')
        cat('Changed Cell Old Value  : ', input$rht_table$changes$changes[[1]][[3]],'\n')
        cat('Changed Cell New Value  : ', input$rht_table$changes$changes[[1]][[4]])
    })
    
    # rht_select_r <- reactive(input$rht_table_select$select)
    # observe({
    #     selectedRow <- rht_select_r()
    #     idx <- selectedRow$r
    #     if(is.null(idx)) {
    #         env4$select_idx <<- NULL ; cat("====> NULL")
    #     } else {
    #         env4$select_idx <<- idx  ; cat("====>",env4$select_idx)
    #     }
    # })
    
    output$resultDisplay3 <- renderPrint({
        env4$select_idx <<- input$rht_table_select$select$r
        cat('Selected Row : ', env4$select_idx,'\n')
    })
}

shinyApp(ui, server)

https://stackoverflow.com/questions/30733573/get-selected-rows-of-rhandsontable

https://code.i-harness.com/en/q/1d4f505

 selectCallback=TRUE을 사용하면 

selected row/ column/range/ cell values 과 edited cells 을 구할수 있다.
( cell을 더블클릭하여 수정한 후, enter를 치면 수정을 완료된다.) 

library(shiny)
library(rhandsontable)
ui=fluidPage(
    rHandsontableOutput('rht_slm_table'),
    verbatimTextOutput('resultDisplay')
)
server=function(input,output,session)({
    df=data.frame(N=c(1:10),L=LETTERS[1:10],M=LETTERS[11:20])
    output$rht_slm_table=renderRHandsontable(
        rhandsontable(df,selectCallback = TRUE,readOnly = FALSE)
    )
    output$resultDisplay=renderPrint({
        cat('Selected Row     :', input$rht_slm_table_select$select$r,'\n')
        cat('Selected Column  :', input$rht_slm_table_select$select$c,'\n')
        cat('Selected Cell Val:', input$rht_slm_table_select$data[[input$rht_slm_table_select$select$r]][[input$rht_slm_table_select$select$c]],'\n')
        cat('Selected Range   :','R',input$rht_slm_table_select$select$r, ' C',input$rht_slm_table_select$select$c, ':',
                                 'R',input$rht_slm_table_select$select$r2,' C',input$rht_slm_table_select$select$c2,sep="",'\n')
        cat('\n')
        cat('Changed Cell Row Column:',input$rht_slm_table$changes$changes[[1]][[1]], input$rht_slm_table$changes$changes[[1]][[2]],'\n')   
        cat('Changed Cell Old Value :',input$rht_slm_table$changes$changes[[1]][[3]],'\n')
        cat('Changed Cell New Value :',input$rht_slm_table$changes$changes[[1]][[4]])
    })
}) # end server
shinyApp(ui = ui, server = server)

 

Categories: R-Shiny

onesixx

Blog Owner

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x