cutebunny
cutebunny

Reputation: 789

shiny: reactive a column in a data frame

I found the package "rhandsontable" is very useful to output table in the shiny. Here is my script:

library(shinydashboard)
library(shiny)
library(data.table)
library(rhandsontable)
DF = data.frame(num = 1:10, price = 1:10,
            stringsAsFactors = FALSE)
ui = fluidPage(
titlePanel("sample"),
fluidRow(box(rHandsontableOutput("hot", height = 400)))  
)
server = function(input, output) {
 output$hot = renderRHandsontable({
 DF$total=DF$num*DF$price
 rhandsontable(DF)
})
}
shinyApp(ui, server)

My question here is when I modify the value in the price column, how can I reactive the value in the total column. To make it clear, if the num is constant, when I change the price from 2 to 4, the value in the total column will automatically change. Does anyone have the solution?

Upvotes: 2

Views: 2995

Answers (2)

Christophe D.
Christophe D.

Reputation: 1089

@PSraj, i dont know if it is what you asked about. But here is the example posted by @Oskar Forsmo with the conversion hot_to_r to get the rhandsontable into a r data frame object.

library(shinydashboard)
library(shiny)
library(data.table)
library(rhandsontable)

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

ui = fluidPage(
  titlePanel("sample"),
  fluidRow(box(rHandsontableOutput("hot", height = 400)),
           textOutput("text1"),
           box(width = 6,dataTableOutput("text2")))  
)
server = function(input, output) {
  data <- reactiveValues(df=DF)

  output$hot <- renderRHandsontable({
    isolate({
      data$df$total       <- data$df$num*data$df$price
      print(sum(data$df$num*data$df$price) )
      data$df$total[11]   <- sum(data$df$num*data$df$price) 
    })
    rhandsontable(data$df, selectCallback = TRUE) 
  })

  observeEvent(input$hot$changes,{
    print('I have change a value')

    # Get changed value
    row.i <- input$hot_select$select$r
    col.i <- input$hot_select$select$c
    new.v <- unlist( input$hot$changes$changes )
    new.v <- new.v[[length(new.v)]]

    data$df[row.i,col.i] <- new.v
    data$df$total <- data$df$num[row.i]*data$df$price[row.i]


    output$text1 <- renderText({ 
      sprintf("You changed line %s value to %s",row.i,new.v)
    })

    output$text2 <- renderDataTable({ 
     hot_to_r(input$hot) #Here we get the dataframe from rhandsontable
                         # You can store it as a reactive variable to do
                         # anything you want
    })

  })



}
shinyApp(ui, server)

Upvotes: 0

RmIu
RmIu

Reputation: 4467

Make your data reactive, bind a change function to your table and then update the values on change (now with sum column):

library(shinydashboard)
library(shiny)
library(data.table)
library(rhandsontable)

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

ui = fluidPage(
  titlePanel("sample"),
  fluidRow(box(rHandsontableOutput("hot", height = 400)))  
)
server = function(input, output) {
  data <- reactiveValues(df=DF)

  output$hot <- renderRHandsontable({
    isolate({
      data$df$total       <- data$df$num*data$df$price
      print(sum(data$df$num*data$df$price) )
      data$df$total[11]   <- sum(data$df$num*data$df$price) 
    })
    rhandsontable(data$df, selectCallback = TRUE) 
  })

  observeEvent(input$hot$changes,{
    print('Change')

    # Get changed value
    row.i <- input$hot_select$select$r
    col.i <- input$hot_select$select$c
    new.v <- unlist( input$hot$changes$changes )
    new.v <- new.v[[length(new.v)]]

    # Save and update the value
    data$df[row.i,col.i] <- new.v
    data$df$total <- data$df$num[row.i]*data$df$price[row.i]

    # Calculate Sum 
    data$df$total[11] <- sum(data$df$total)
  })

}
shinyApp(ui, server)

Upvotes: 1

Related Questions