使用闪亮的 R 工作室中的操作按钮填充 rhandsontable 对象的缺失值

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

我有以下代码。单击 "go" 按钮后,我想用粗体数字(或可能是红色)填充 rhandsontable 对象的缺失值,考虑到计算值的关系

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

所以输出 table 是没有缺失值的 rhandsontable(缺失值已被替换)。有谁知道我该怎么做?非常感谢你。 :)

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)

这是你想要的吗?我添加了 observeEvent 来触发按钮并重新绘制 table。我还将您的数据集包装到 reactiveValues 中,因此更易于操作

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)

编辑:以粗体形式添加 NA,并将 NA 替换为通过等式

计算的值
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)

点击前:

点击后,NA值变为18.40: