jrowen / rhandsontable

A htmlwidgets implementation of Handsontable.js
http://jrowen.github.io/rhandsontable/
Other
380 stars 147 forks source link

Retrieve data only when the table is not empty #406

Open mtopart opened 2 years ago

mtopart commented 2 years ago

Hi,

Sorry in advance for my English.

I am having difficulty when I want to automate the following code.

I'd like the output$text_ch to appear on the condition that the array is filled (sum(df$values[[2]] and sum(df$values[[2]] both greater than zero) rather than it you have to click on the send button.

What to do ? Thank you in advance

`if (interactive()) {
  library(shiny)
  library(bs4Dash)
  library(rhandsontable)
  library(glue)

  shinyApp(
    ui = dashboardPage(
      header = dashboardHeader(),
      body = dashboardBody(
        fluidRow(
          box(
            title = "Charges",
            rHandsontableOutput(outputId = "tabelle"),
            textOutput("texte_ch"),
            br(),
            actionButton("button_send", 
                         label = "", 
                         icon = icon("paper-plane"),
                         color = "primary"))

      )
      ),
      sidebar = dashboardSidebar()
    ),
    server = function(input, output, session) {

      Charges <- c("", "","","","", "", "", "", "", "")
      Mini <- c("", "","","","", "", "", "", "", "") %>% 
        as.numeric()
      Maxi <- c("", "","","","", "", "", "", "", "") %>% 
        as.numeric()

      df <- data.frame(Charges, 
                       Mini, 
                       Maxi)

      output$tabelle <- renderRHandsontable({
        rhandsontable(data = df)
      }) 

      observeEvent(eventExpr = input$button_send, {
        df$values <- hot_to_r(input$tabelle)

        sum_mini <- df$values[[2]] %>% 
          sum(., na.rm = TRUE)

        sum_maxi <- df$values[[3]] %>% 
          sum(., na.rm = TRUE)

        output$texte_ch <- renderText({
          glue('Les charges varient de {sum_mini} à {sum_maxi} (€ ou k€).')  

        })
      })      

    }
  )
}`