CJJ
CJJ

Reputation: 77

renderRHandsontable how to update table based on values in one column of table

I am not even sure if my desired output is possible, but wanted to ask in case it is because I am very stuck on this issue.I am building a shiny app where I have an rHandsontableOutput object. When I update the 4th column of the table, I want it to automatically multiply the 5-16 columns of the table by the new values in the 4th column. I'm having difficulty getting my multiplied results to display in my table. rHandsontable has been a little tricky for me to grasp. I know how to display the data in a table and then use it in another output object, but I am a little confused on how to use it in the same output object. It would preferred to not have to resort to text input to then update the table or some other type of solution that is not in a table format.

I have referenced a few other posts with no success yet.

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?

My code is as follows:

#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)

})

I have also tried updating the table with the hit of a button or creating just one vector that can be edited and binding it with the new table, but I have had no luck with either solution yet.

Upvotes: 0

Views: 851

Answers (1)

FMM
FMM

Reputation: 2015

If your problem is multiplying the columns, you could try the following:

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

Created on 2019-05-05 by the reprex package (v0.2.1)

You basically use the function dplyr::mutate_at to refer to the columns you want to be multiplied. Then, we use the anonymous function(x) to multiply column a by the columns specified before.

Upvotes: 0

Related Questions