Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Shiny App: Reactive/Calculate column in Rhandsontable

Tags:

shiny

I am using Rhansontable package, i want to create a table where if i change value in one column then another column automatically calculate. For example:

DF = data.frame(num = 1:10, price = 1:10,stringsAsFactors = FALSE)

In above dataframe another column "Total" (num*price) should calculate automatically when i change the value in column "num"

Can please help with sample shiny code?

like image 272
Amar AAA Avatar asked Mar 08 '17 07:03

Amar AAA


People also ask

How to display column with HTML data in shiny apps?

For shiny apps, use renderer = htmlwidgets::JS ("safeHtmlRenderer") to display columns with html data. The allowed html tags default to <em><b><strong><a><big>, but the (hidden) allowedTags parameter can in rhandsontable can be used to customize this list.

What are reactive expressions in shiny apps?

Shiny apps wow your users by running fast, instantly fast. But what if your app needs to do a lot of slow computation? This lesson will show you how to streamline your Shiny apps with reactive expressions. Reactive expressions let you control which parts of your app update when, which prevents unnecessary computation that can slow down your app.

What is rhandsontable?

rhandsontable is a htmlwidget based on the handsontable.js library. Handsontable is a data grid component with an Excel-like appearance. Built in JavaScript, it integrates with any data source with peak efficiency. It comes with powerful features like data validation, sorting, grouping, data binding, formula support or column ordering.

How do you use rhandsontable in R?

rhandsontable attempts to map R classes to an appropriate handsontable type. Factors will be mapped to dropdown, with the choices specified by level and allowInvalid set to FALSE. To allow new levels, set allowInvalid to TRUE (using hot_col; it may also be desirable to set strict to FALSE ).


1 Answers

I think this is what you want

#rm(list = ls())
library(shiny)
library(rhandsontable)

## Create the dataset
DF = data.frame(num = 1:10, price = 1:10,Total = 1:10,stringsAsFactors = FALSE)
numberofrows <- nrow(DF)

server <- shinyServer(function(input, output, session) {

  # Initiate your table
  previous <- reactive({DF})

  MyChanges <- reactive({
    if(is.null(input$hotable1)){return(previous())}
    else if(!identical(previous(),input$hotable1)){
      # hot.to.df function will convert your updated table into the dataframe
      mytable <- as.data.frame(hot_to_r(input$hotable1))
      # here the second column is a function of the first and it will be multipled by 100 given the values in the first column
      mytable <- mytable[1:numberofrows,]

      # Add some test cases
      mytable[,1][is.na(mytable[,1])] <- 1
      mytable[,2][is.na(mytable[,2])] <- 1
      mytable[,3] <- mytable[,1]*mytable[,2]
      mytable
    }
  })
  output$hotable1 <- renderRHandsontable({rhandsontable(MyChanges())})
})

ui <- basicPage(mainPanel(rHandsontableOutput("hotable1")))
shinyApp(ui, server)

enter image description here

like image 115
Pork Chop Avatar answered Dec 08 '22 00:12

Pork Chop



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!