0

In the below code, I'm trying to understand what the input[[...]] in the two commented observeEvent() functions do. Could someone please explain?

I'm trying to adapt this code so the function of the individual delete action buttons rendered under each added table can be consolidated into a single selectInput() or selectizeInput() box, showing all tables available for deletion, and then I can remove these individual table delete buttons. I need to understand this input[[...]] part in the below code first, so perhaps I can do the equivalent with selectInput() or selectizeInput(). I've only used input$... before in referring to action buttons or other user input widgets for an observeEvent().

Code:

library(shiny)
library(rhandsontable)

data1 <- data.frame(row.names = c("A","B","C","Sum"),"Col 1"=c(1,1,0,2),check.names=FALSE)

ui <- fluidPage(
  br(),
  actionButton("addTbl","Add table"),
  br(),br(),
  tags$div(id = "placeholder",        
           tags$div(
             style = "display: inline-block", 
             rHandsontableOutput("hottable1")
           )
  )
)

server <- function(input, output, session) {
  uiTbl <- reactiveValues(div_01_tbl = data1) # mod
  rv <- reactiveValues()                
  
  observeEvent(input$hottable1, {uiTbl$div_01_tbl <- hot_to_r(input$hottable1)}) # mod
  
  output$hottable1 <- renderRHandsontable({rhandsontable(uiTbl$div_01_tbl, useTypes = TRUE)}) # mod
  
  observeEvent(input$addTbl, {
    divID <- paste0("div_", if(input$addTbl+1 < 10){"0"},input$addTbl+1) # mod
    dtID <- paste0(divID, "_DT")
    btnID <- paste0(divID, "_rmv")
    uiTbl[[paste0(divID,"_tbl")]] <- data1 # captures initial dataframe values
    
    insertUI(
      selector = "#placeholder",
      ui = tags$div(
        id = divID,
        style = "display:inline-block;",
        rHandsontableOutput(dtID), 
        actionButton(btnID, "Delete", class = "pull-left btn btn-danger"),
      )
    )
    
    output[[dtID]] <- renderRHandsontable({
      req(uiTbl[[paste0(divID,"_tbl")]])
      rhandsontable(uiTbl[[paste0(divID,"_tbl")]], useTypes = TRUE)
    })
    
    # print(btnID)
    
    # what does input[[dtID]] below do?
    observeEvent(input[[dtID]], {uiTbl[[paste0(divID,"_tbl")]] <- hot_to_r(input[[dtID]])})
    
    # what does input[[btnID]] below do?
    observeEvent(input[[btnID]],{
      removeUI(selector = paste0("#", divID))
      rv[[divID]] <- NULL
      uiTbl[[paste0(divID,"_tbl")]] <- NULL
    },
    ignoreInit = TRUE,
    once = TRUE
    )
  })
  
  
  observe({
    tables_list <- reactiveValuesToList(uiTbl)
    tables_list <- tables_list[order(names(tables_list))]
    table_lengths <- lengths(tables_list)
    cumsum_table_lengths <- cumsum(table_lengths)[table_lengths != 0L]
    
    for(i in seq_along(cumsum_table_lengths)){
      names(uiTbl[[names(cumsum_table_lengths[i])]]) <- paste("Col", cumsum_table_lengths[i])
    }
  })
}

shinyApp(ui, server)
4
  • 3
    You created dtID as an object dtID <- paste0(divID, "_DT"), thus to evaluate the value, you need [[, instead of $ as $ does literal evaluation i.e. checking if there is dtID instead of the value from it Commented Dec 29, 2022 at 21:44
  • 3
    It is similar to extracting columns from the input data i.e. v1 <- "Col 1"; > data1$v1# NULL whereas data1[[v1]]# [1] 1 1 0 2 or data1$"Col 1" [1] 1 1 0 2 Commented Dec 29, 2022 at 21:46
  • 1
    Related: stackoverflow.com/q/1169456/3358272 (comparison of [ and [[ and $). Commented Dec 30, 2022 at 0:25
  • 1
    input[[dtID]] and input[[btnID]] are passed as observeEvent's eventExpr. Accordingly observeEvent is triggered to run after those inputs changed. Commented Jan 5, 2023 at 10:02

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.