【问题标题】:Rshiny renderRHandsontable how to update table based on values in one column of tableR Shiny renderRHandsontable如何根据表格一列中的值更新表格
【发布时间】:2019-09-22 09:44:00
【问题描述】:

我什至不确定我想要的输出是否可能,但我想问一下,以防万一,因为我非常卡在这个问题上。我正在构建一个闪亮的应用程序,其中有一个 rHandsontableOutput 对象。当我更新表的第 4 列时,我希望它自动将表的 5-16 列乘以第 4 列中的新值。我很难让我的 multiplied 结果显示在我的表格中。 rHandsontable 对我来说有点难以掌握。我知道如何在表格中显示数据,然后在 another 输出对象中使用它,但我对如何在同一个输出对象中使用它有点困惑。它宁愿不必求助于文本输入来更新表格或某种其他类型的非表格格式的解决方案。任何帮助或替代解决方案将不胜感激!

我参考了其他一些帖子,但都没有成功。

show the sum of specific columns based on rhandsontable values

Update handsontable by editing table and/or eventReactive

Flexdashboard, rhandsontable: how to programmatically access user updated table?

我的代码如下:

#ui

... fluidRow(
              column(width=8,
                     box(
                       width = NULL, div(rHandsontableOutput('contents'), style = "font-size: 92%"),title = "Potential View",
                       status = "primary", solidHeader = TRUE)
                     # img(height = "500px", alt="SNAP Logo", src="overlay.png"))

              )


            )...

#server
indat <- reactiveValues(data=table_1)

observe({
  if(!is.null(input$contents))
    indat$data <- hot_to_r(input$contents)

})


output$contents <- renderRHandsontable({

  tbl <- rhandsontable(indat$data) %>% hot_col(col = c(1,2,3), readOnly = TRUE) 

  c=as.vector(tbl$col_4)
  tbl_new = tbl[,c(5:16)]
  test= data.frame(mapply(`*`,tbl_new,c))

  return(test)

})

我也尝试过点击按钮更新表格,或者只创建一个可编辑的矢量并将其与新表格绑定,但我对这两种解决方案都没有运气。

【问题讨论】:

    标签: r shiny shiny-reactivity rhandsontable shinyapps


    【解决方案1】:

    如果您的问题是列相乘,您可以尝试以下方法:

    library(dplyr)
    #> 
    #> Attaching package: 'dplyr'
    #> The following objects are masked from 'package:stats':
    #> 
    #>     filter, lag
    #> The following objects are masked from 'package:base':
    #> 
    #>     intersect, setdiff, setequal, union
    
    df <- tibble(
      a = 1:10,
      b = 11:20,
      c = 21:30,
      d = 31:40
    )
    
    df
    #> # A tibble: 10 x 4
    #>        a     b     c     d
    #>    <int> <int> <int> <int>
    #>  1     1    11    21    31
    #>  2     2    12    22    32
    #>  3     3    13    23    33
    #>  4     4    14    24    34
    #>  5     5    15    25    35
    #>  6     6    16    26    36
    #>  7     7    17    27    37
    #>  8     8    18    28    38
    #>  9     9    19    29    39
    #> 10    10    20    30    40
    
    df %>% 
      mutate_at(2:4, function(x) x * .$a)
    #> # A tibble: 10 x 4
    #>        a     b     c     d
    #>    <int> <int> <int> <int>
    #>  1     1    11    21    31
    #>  2     2    24    44    64
    #>  3     3    39    69    99
    #>  4     4    56    96   136
    #>  5     5    75   125   175
    #>  6     6    96   156   216
    #>  7     7   119   189   259
    #>  8     8   144   224   304
    #>  9     9   171   261   351
    #> 10    10   200   300   400
    

    reprex package (v0.2.1) 于 2019 年 5 月 5 日创建

    您基本上使用函数dplyr::mutate_at 来引用您想要相乘的列。然后,我们使用匿名function(x) 将列a 与之前指定的列相乘。

    【讨论】:

    • 感谢您的回复。我的问题不在于将列相乘,而是将相乘的结果显示在我的 rhandontable 表对象中。
    猜你喜欢
    • 2018-09-13
    • 2019-08-03
    • 2020-06-27
    • 1970-01-01
    • 1970-01-01
    • 2021-08-28
    • 2018-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多