3

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)
Tonio Liebrand
  • 17,189
  • 4
  • 39
  • 59
Sumeda
  • 115
  • 8

1 Answers1

2

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

Pork Chop
  • 28,528
  • 5
  • 63
  • 77
  • thank you very much. Instead of pink background how can I modify it by bold NA? – Sumeda May 18 '17 at 09:49
  • Moreover , how can I add this equation datacopy[16, "wt"] = datacopy[16, "mpg"] + datacopy[16, "cyl"] to compute the missing value when I click "go" button? – Sumeda May 18 '17 at 09:52
  • This is not what I want. When you click the "Go" button the missing value should be evaluated according to the equation datacopy[16, "wt"] = datacopy[16, "mpg"] + datacopy[16, "cyl"] – Sumeda May 18 '17 at 10:03
  • or without clicking any button when manually you edit a value of a table the other two variables should be changed following the equation datacopy[, "wt"] = datacopy[, "mpg"] + datacopy[, "cyl"] – Sumeda May 18 '17 at 10:05
  • thank you very much. It is very useful. Thank you for helping :) – Sumeda May 18 '17 at 12:37
  • Please accept the answer if that is what you wanted for others to reference. – Pork Chop May 18 '17 at 12:40
  • Ok. But how can I make 18.40 bold ? – Sumeda May 19 '17 at 07:02