1

I'm writing a 3D stock portfolio model with R using Shiny

The ui.R is:

# UI

# Libraries
library(shiny)

# Interface
shinyUI(fluidPage(
 titlePanel("3D Stock Projection"),

 sidebarLayout(
    sidebarPanel(
            helpText("Select a stock to examine. 
        Information will be collected from yahoo finance."),

            textInput("symb", "Symbol", "SPY"),

            dateRangeInput("dates",
                "Date range",
                start = "2013-01-01",
                end = as.character(Sys.Date())),

            actionButton("get", "Get Stock"),

            br(),
            br(),

          checkboxInput("log", "Plot y axis on log scale",
                value = FALSE),

            checkboxInput("adjust", "Adjust prices for inflation", 
            value = FALSE)
        ),

    mainPanel(plotOutput("plot"))
 )
 ))

the server.R is:

# Server 

# Libraries
 library(shiny)
 library(TTR)
 library(scales)
 library(quantmod)
 library(scatterplot3d)

#Equations
 shinyServer(function(input, output) {

 #This finds holding period
  time <- reactive({
    getSymbols(input$symb, 
        src="yahoo", 
            from = input$dates[1],
            to = input$dates[2],
            auto.assign = FALSE)
  })

 #Basic yield equaition in percentage
  yield <- reactive({
    initial <- as.number(getQuote(input$symb,
            src="yahoo",
            from = input$date[1],
            auto.assign = FALSE)
        )
    final <- as.number(getQuote(input$symb, 
            src="yahoo",
            from = input$date[2],
            auto.assign = FALSE)
        )       
    as.number(percent(((final-initial)/initial)))
  })

 #This finds the alpha by retrieving the S&P yield  
  alpha <- reactive({
    marketInitial <- as.number(getQuote("S&P500",
                src="yahoo",
                from = input$date[1],
                auto.assign = FALSE)
            )
    marketFinal <- as.number(getQuote("S&P500",
                src="yahoo",
                from = input$date[2],
                auto.assign = FALSE)
            )
    as.number(percent(((marketFinal-marketInitial)/marketInitial)))     
  })    

 #3D Projection         
  output$plot <- renderPlot({
    scatterplot3d(time, yield, alpha, 
            highlight.3d = TRUE,
            col.axis = "blue",
            col.grid = "ligthblue",
            main = "Helix",
            pch = 20)
  })
 })

When I run it the interface opens up successfully but the plot just doesn't appear, and instead it gives me an error saying that I cannot cannot "coerce type 'closure' to vector of type 'double'"

I think the problem is the date system and the way I am calculating time, which is difficult because I have to manage dates as opposed to numbers and none of the other instances of this problem on Stackoverflow address the closure issue with something as complicated as a date.

I wanna thank everyone for your help in advance XD

  • 1
    You should call reactive expressions using parentheses, e.g. `scatterplot3d(time(), yield(), alpha(),...` – Marat Talipov Feb 23 '15 at 04:06
  • `as.number` or `as.numeric`? – Metrics Feb 23 '15 at 04:21
  • There are multiple problems here. In addition to using the reactive expressions and `as.numeric` you are being very loose with your data. `time` is returning a `data.frame` with multiple rows where `yield` and `alpha` are a single row. So you cannot plot your 3d scatterplot. You are also forcing these rows (which contain POSIxt and character data). You should first look at exactly what you want in your plot and update your question as it is not currently answerable. – cdeterman Feb 23 '15 at 16:14
  • thank for pointing out the `as.numeric` issue and the way I was calling the reactive functions. Those are now fixed XD Can I fix the issue of `time` with `as.Date.numeric`? I am trying to display time in intervals on the x-axis. – Lorenzo Barberis Canonico Feb 24 '15 at 05:12
  • It is not possible to reproduce your example. Can you please edit your code above to correct the reactive function calls, use as.numeric and correct the input$date by input$dates instead. Also the getQuote function you are using is not accepting the attribute "from", so alpha and yield are full of NAs. Also variable times cannot be used directly, you should use index(times) as explained here (http://stackoverflow.com/questions/9668239/r-obtaining-rownames-date-using-quantmod). – faidherbard Apr 24 '15 at 13:16

0 Answers0