Sumeda
Sumeda

Reputation: 115

Fill up missing values of rhandsontable object using an action button in shiny R studio

I have the following code. I want to fill the missing value of the rhandsontable object with a bolded number (or may be with red color) once the "go" button is clicked, given the relationship of calculating the value as

datacopy[16, "wt"]= datacopy[16, "mpg"] + datacopy[16, "cyl"]

So the output table is to be rhandsontable with no missing value (missing value been replaced). Does anyone know how I can do this? thank you very much. :)

library(shiny)
library(datasets)
library(rhandsontable)

ui=fluidPage(

br(), br(), actionButton("update", "go", class="success"), br(), br(),  
rHandsontableOutput("table1")
)


server=function(input, output, session) {

mt=reactive({

datacopy= data.table(mtcars)
datacopy[16, "wt"] <- NA
datacopy

})

output$table1=renderRHandsontable({
rhandsontable(mt())
})

}

shinyApp(ui,server)

Upvotes: 3

Views: 540

Answers (1)

Pork Chop
Pork Chop

Reputation: 29387

Is this what you want? I had added the observeEvent to trigger the button and repaint the table. I also wrapped your dataset into reactiveValues so its easier to manipulate

library(shiny)
library(rhandsontable)

ui <- fluidPage(

  br(), br(), actionButton("update", "go", class="success"), br(), br(),  
  rHandsontableOutput("table1")
)

server=function(input, output, session) {

  mydata <- reactiveValues()
  mydata$data <- mtcars

  observeEvent(input$update,{
    mydata$data[16, "wt"] <- NA
  })

  output$table1 <- renderRHandsontable({
    rhandsontable(mydata$data) %>%
      hot_cols(renderer = "
               function (instance, td, row, col, prop, value, cellProperties) {
               Handsontable.renderers.NumericRenderer.apply(this, arguments);
               if (value== 'NA') {
               td.style.background = 'pink';} 
               }")
  })
}

shinyApp(ui,server)

enter image description here

Edit: Add the NA in Bold and substitute NA the value calculated by the equation

library(shiny)
library(rhandsontable)

ui <- fluidPage(
  br(), br(), actionButton("update", "go", class="success"), br(), br(),  
  rHandsontableOutput("table1")
)

server=function(input, output, session) {
  mtcars[16, "wt"] <- NA

  mydata <- reactiveValues()
  mydata$data <- mtcars
  #mydata$data[16, "wt"] <- NA

  observeEvent(input$update,{
    mydata$data[16, "wt"] <- mydata$data[16, "mpg"] + mydata$data[16, "cyl"] 
  })

  output$table1 <- renderRHandsontable({
    rhandsontable(mydata$data) %>%
      hot_cols(renderer = "
               function (instance, td, row, col, prop, value, cellProperties) {
               Handsontable.renderers.NumericRenderer.apply(this, arguments);
               if (value== 'NA') {
               td.style.fontWeight = 'bold';} 
               }")
  })
  }

shinyApp(ui,server)

Before the Click: enter image description here

After the Click, the value with NA changed to 18.40:

enter image description here

Upvotes: 2

Related Questions