Rajarshi Bhadra
Rajarshi Bhadra

Reputation: 1944

Replacing values and recalculating in reactive tables

I have a table with three columns: Sales,Prices and Revenue. I want to create an app which will ask for user input regarding prices but when no input has been i.e before the action button is pressed the chart will calculate the revenue in terms of some base prices already in the code. I am unable to generate this reactive. I have provided the code below. Any help will be greatly appreciated.

ui.R

library(shiny)
library(googleVis)
shinyUI(fluidPage(
  titlePanel("Test"),
  sidebarLayout(
    sidebarPanel(
      numericInput(inputId = "A", label = "A",value=0),
      numericInput(inputId = "B", label = "B",value=0),
      numericInput(inputId = "C", label = "C",value=0),
      numericInput(inputId = "D", label = "D",value=0),
      actionButton("action","Submit")
    ),
    mainPanel(
      htmlOutput("mytable")
    )
  )
))

server.R

library(data.table)
library(googleVis)
library(shiny)
shinyServer(function(input, output) {
  Sales<-c(2,6,9,10)
  Prices<-c(34,43,76,89)
  Prices_user<-reactive({
    c(input$A,input$B,input$C,input$D)
  })
  Rev<-Sales*Prices
  Rev_user<-reactive({
    Sales*Prices_user()
  })

  combined<-data.frame(Sales,Prices,Rev)
  combined_user<-reactive({
    data.frame(Sales,Prices_user(),Rev_user())
  })
  output$mytable<-renderGvis({  
    if(input$action ==0){
      gvisTable(combined)
      }else{
        gvisTable(combined_user())
      }

  })  
})

Upvotes: 0

Views: 276

Answers (1)

rsoren
rsoren

Reputation: 4206

I would modify your server.R like this (untested):

library(data.table)
library(googleVis)
library(shiny)
shinyServer(function(input, output) {

  output$mytable <- ifelse(input$action != 0,

    Sales<-c(2,6,9,10)
    Prices <-reactive({
      c(input$A,input$B,input$C,input$D)
    })
    Rev<-reactive({
      Sales*Prices()
    })
    combined<-data.frame(Sales,Prices,Rev)
    renderGvis({
      gvisTable(combined)
    }),

    Sales<-c(2,6,9,10)
    Prices<-c(34,43,76,89)
    Rev<-reactive({
      Sales*Prices()
    })
    combined<-data.frame(Sales,Prices,Rev)
    renderGvis({
      gvisTable(combined)
    })

  ) 
})

Upvotes: 1

Related Questions