这是你想要的吗?我添加了observeEvent
触发按钮并重新绘制表格。我还将您的数据集包装到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: