How to make a row with dropdown list visible and reactive?

45 Views Asked by At

I have an rhandsontable with five variables. I have assigned dropdown lists to some of the columns and want the table to be reactive when I choose an option from the dropdown lists. When I run the script in RStudio console it is giving me the folloving error-message:

"Error in :: htmlwidgets shinyRenderWidget: object 'selectedcolumn_a2' not found"

I can't figure out what I am doing wrong. Can someone help me find and fix an incorrect part of the script below?

DF1 <- data.table(
        column_a1 = NA_character_,
        column_a2 = NA_character_,
        column_a3 = NA,
        column_a4 = NA,
        column_a5 = as.numeric()
        )

column_a1Options <- as.character(c(NA_character_, "Option_a1", "Option_a2"))
column_a2Options <- as.character(c(NA_character_, "Method_a1", "Method_a2"))
column_a3Options <- as.numeric(c(NA, 2, 3))
column_a4Options <- as.numeric(c(NA, 4,5))

ui <- dashboardPage(
    dashboardHeader(title = "SAMPLE"),
    dashboardSidebar(
        menuItem("Calculation", tabName = "calculation",
                               menuSubItem("Addition", tabName = "table1"))),

      dashboardBody(
        tabItems(
          tabItem(
        tabName = "table1",
            column(
              "Calculation table",
          width=6,
              rHandsontableOutput("Table1")
            ),
          )
        )
      )
    )
server <- function(input, output) {

  DF2 <- reactiveVal(data.frame(DF1))

  observeEvent(input$Table1, {
        DF2(hot_to_r(input$Table1))
  })


output$Table1 <- renderRHandsontable({

  DF3 <- rhandsontable(DF2(), stretchH = "all", selectCallback = TRUE, height = 300) %>%
    hot_col("column_a1", allowInvalid = FALSE, type = "dropdown", source=NA_character_, readOnly=TRUE) %>%
    hot_col("column_a2", allowInvalid = FALSE, type = "dropdown", source=NA_character_, readOnly = TRUE) %>%
    hot_col("column_a3", allowInvalid = FALSE, type = "dropdown", source=NA, readOnly=TRUE) %>%
    hot_col("column_a4", allowInvalid = FALSE, type = "dropdown", source=NA, readOnly=TRUE)



  if (!is.null(input$Table1_select$select$r)) {
    selectedcolumn_a1 <- DF2()[input$Table1_select$select$r, "column_a1"]
    selectedcolumn_a2 <- DF2()[input$Table1_select$select$r, "column_a2"]
    selectedcolumn_a3 <- DF2()[input$Table1_select$select$r, "column_a3"]
    selectedcolumn_a4 <- DF2()[input$Table1_select$select$r, "column_a4"]
    }

DF3 <- if(selectedcolumn_a2 == "Method_a1") {
        DF3 %>%
    hot_col(col = "column_a1", allowInvalid = FALSE, type = "dropdown", source = column_a1Options) %>% 
        hot_cell(row = input$Table1_select$select$r, col = "column_a1", readOnly = FALSE) %>%
        hot_col(col = "column_a2", allowInvalid = FALSE, type = "dropdown", source = column_a2Options) %>% 
        hot_cell(row = input$Table1_select$select$r, col = "column_a2", readOnly = FALSE) %>%
        hot_col(col = "column_a3", allowInvalid = FALSE, type = "dropdown", source = column_a3Options) %>% 
        hot_cell(row = input$Table1_select$select$r, col = "column_a3", readOnly = FALSE) %>%
        hot_col(col = "column_a4", allowInvalid = FALSE, type = "dropdown", source = column_a4Options) %>% 
        hot_cell(row = input$Table1_select$select$r, col = "column_a4", readOnly = FALSE) %>%
        hot_table(contextMenu = FALSE)
 }

  observe({
    DF3$column_a5 <- ifelse(
        selectedcolumn_a1 == Option_a1  &
        is.na(selectedcolumn_a2) &
        selectedcolumn_a3 == 2 &
        selectedcolumn_a4 == 5,
    DF3$column_a3 + DF3$column_a4
    )
  })    
 DF3  
})
}

shinyApp(ui = ui, server = server)
0

There are 0 best solutions below