【问题标题】:Fill up missing values of rhandsontable object using an action button in shiny R studio使用闪亮 R 工作室中的操作按钮填充 rhandsontable 对象的缺失值
【发布时间】:2017-10-11 12:20:32
【问题描述】:

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

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

所以输出表是可随机排列的,没有缺失值(缺失值已被替换)。有谁知道我该怎么做?非常感谢您。 :)

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)

【问题讨论】:

    标签: r shiny rstudio rhandsontable


    【解决方案1】:

    这是你想要的吗?我添加了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:

    【讨论】:

    • 非常感谢。我怎样才能用粗体NA修改它而不是粉红色背景?
    • 此外,当我点击时,如何添加这个方程 datacopy[16, "wt"] = datacopy[16, "mpg"] + datacopy[16, "cyl"] 来计算缺失值“开始”按钮?
    • 这不是我想要的。当您单击“开始”按钮时,应根据等式 datacopy[16, "wt"] = datacopy[16, "mpg"] + datacopy[16, "cyl"] 评估缺失值
    • 或在手动编辑表格值时不单击任何按钮,其他两个变量应按照等式 datacopy[, "wt"] = datacopy[, "mpg"] + datacopy[ 进行更改, "圆柱"]
    • 非常感谢。这是非常有用的。谢谢你的帮助:)
    猜你喜欢
    • 2017-01-30
    • 2017-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-12
    • 2020-03-22
    • 2013-11-19
    • 1970-01-01
    相关资源
    最近更新 更多