Interactive Cobb-Douglas Web App with R

Screen Shot 2014-11-04 at 7.24.02 PM

I used Shiny to make an interactive cobb-douglass production surface in R. It reacts to user’s share of labor and capital inputs and allows the user to rotate the surface. The contour plot (isoquants) is also dynamic.

Shiny works using two R codes stored in the same folder. One R code works on the user interface (UI) side and the other works on the server side.

On the UI side, I take user inputs for figure rotations and capital/labor inputs via slidebars and output a plot of the surface and isoquants.

library(shiny)

shinyUI(pageWithSidebar(
  headerPanel("Cobb-Douglas Production Function"),
  sidebarPanel(
    sliderInput("L","Share of Labor:",
                min=0, max=1, value=.5, step=.1),
    sliderInput("C","Share of Capital:",
                min=0, max=1, value=.5, step=.1),  
    sliderInput("p1","Rotate Horizontal:",
              min=0, max=180, value=40, step=10),
    sliderInput("p2","Rotate Vertical:",
            min=0, max=180, value=20, step=10)
    ),
  mainPanel( plotOutput("p"),
             plotOutput('con'))  
))

The manipulation of the inputs is done on the server side:

library(shiny)
options(shiny.reactlog=TRUE)

shinyServer(function(input,output){
  observe({
  # create x, y ranges for plots
  x=seq(0,200,5)
  y=seq(0,200,5)
  
  # Cobb-Douglass Model
  model<-  function(a,b){
      (a**(as.numeric(input$L)))*(b**(as.numeric(input$C)))
  }

  # generate surface values at given x-y points
  z<-outer(x,y,model)

  # create gradient for surface
  pal<-colorRampPalette(c("#f2f2f2", "Blue"))
  colors<-pal(max(z))
  colors2<-pal(max(y)) 
  
  # plot functions
  output$p<-renderPlot({persp(x,y,z,
                              theta=as.numeric(input$p1),
                              phi=as.numeric(input$p2),
                              col=colors[z],
                              xlab="Share of Labor",
                              ylab="Share of Capital",
                              zlab="Output"
                      )})
  output$con<-renderPlot({contour(x,y,z,
                              theta=as.numeric(input$p1),
                              phi=as.numeric(input$p2),
                              col=colors2[y],
                              xlab="Share of Labor",
                              ylab="Share of Capital"
  )})
  })
})