0
votes

I am trying to build a shiny app of a project which I have finished implementing in R locally. I am having issues with the syntax of the said implementation, particularly, with reactive inputs.

When I wrote the code locally, I have three User Defined Functions- AnalysisofReturn, Visualizations and MCS, with their respective inputs. The first one, AnalysisofReturn takes in a string for input, from within the list ("Daily","Weekly","Monthly","Quarterly","Yearly") and work with that from there. If I can understand what I am doing wrong in this one instance, I am quite sure I can implement the other two functions on my own. My first trial was to keep my base code separate from ui.R and server.R, which did not work. Then I took the UDF into server.R, and it showed promise. It is my understanding that the render family of functions is reactive by default, and since the function I am using is a UDF, I needed to force it into a reactive state. Within the UDF, I made the call to my dataframe, "myTable" as I should call a function, which resolved the subsetting issue I was getting. I got the code running at one point, when I removed the UDF as such, and did the operations separately, but the table was not being rendered in that case. But it ought to work with the UDF, since I shall want to create more of them in the future, and I don't want to write a code which is disorganized. I shall add a chunk of the dataset herewith. I am using shinydashboard as template, which has no issue generating me the page itself.

Frequency   Date        ETF Index     Underlying Index

Daily       02/06/2009  60            1361.36
Daily       03/06/2009  56.41         1338
Daily       04/06/2009  57.27         1328.86
Daily       05/06/2009  55.72         1321.77

Weekly      05/06/2009  55.72         1321.77
Weekly      12/06/2009  58.38         1347.5
Weekly      19/06/2009  54.72         1305.8
Weekly      26/06/2009  54.74         1305.82

Monthly     30/06/2009  54.26         1307.16
Monthly     31/07/2009  65.28         1425.4
Monthly     31/08/2009  70.71         1498.97
Monthly     30/09/2009  76.18         1552.84

Quarterly   30/06/2009  54.26         1307.16
Quarterly   30/09/2009  76.18         1552.84
Quarterly   31/12/2009  77.99         1580.77
Quarterly   31/03/2010  79.96         1584.28

Yearly      31/12/2009  77.99         1580.77
Yearly      31/12/2010  85.23         1658.3
Yearly      30/12/2011  58.8          1412.55
Yearly      31/12/2012  79.11         1604

ui.R

dHeader <- dashboardHeader(title="Leveraged ETF")

dSidebar <- dashboardSidebar(

                            selectInput(
                              "Frequency",
                              label = h3("Frequency"),
                              choices = list("Daily" = "Daily", "Weekly" = "Weekly", "Monthly" = "Monthly", "Quarterly" = "Quarterly", "Yearly" = "Yearly"),
                              selected = "Yearly"),

                            selectInput(
                              "Task",
                              label = h3("Task"),
                              choices = list("Back Testing" = "backTesting", "Monte Carlo Simulation" = "monteCarloSimulation"),
                              selected = "Back Testing"),

                            conditionalPanel("input.Task=='backTesting'",
                                             selectInput("displayedOutputs",label=h3("Results"),
                                                         choices=list("First 9 values"="nineValues","Summary Statistics"="summaryStatistics","Charts"="charts"),
                                                         selected="First 9 values")),

                            conditionalPanel("input.Task=='monteCarloSimulation'",
                                             selectInput("displayedOutputs",label=h3("Results"),
                                                         choices=list("Simulations"="simulations","Simulation Graph"="simulationGraph"),
                                                         selected="Simulations"))

           )

dBody <- dashboardBody(tableOutput('Results')

)
ui <-     dashboardPage(dHeader,dSidebar,dBody)

server.R

server <- function(input, output, session) {
    #______________________________________________________________
    AnalysisofReturn <- function(Freq)                                          #Function to calculate all the relevant, time point-to-time point data
    {

        setwd("//SPVIPH07/testvip0002/03.Individual/Ramachandran/Shiny Practice")

        InputData <- as.data.frame(read.csv("Input/Dataset for R.csv",header=TRUE))

        myTable <- reactive({subset(InputData,ï..Frequency==Freq)})

        myTable()$ï..Frequency <- NULL

        myTable()$Date <- as.Date(myTable()$Date,format='%d/%m/%Y',origin="1970-01-01")

        myTable()$ETF.Return <- as.numeric(c("-",diff(log(myTable()$ETF.Index))))

        myTable()$Index.Return <- as.numeric(c("-",diff(log(myTable()$Underlying.Index))))

        myTable()$Multiple <- myTable()$ETF.Return/myTable()$Index.Return
        myTable()$Non.Regular.Movement <- ifelse(myTable()$Multiple<0,1,0)
        myTable()$Non.Regular.Positive.Movements <- ifelse(myTable()$Non.Regular.Movement==1 & myTable()$ETF.Return>0,1,0)
        myTable()$Absolute.Deviation.From.Ideal.In.Basis.Points <- round(abs(myTable()$ETF.Return-(2*myTable()$Index.Return))*10000,digits=0)


    }


    output$Results <- renderDataTable({AnalysisofReturn(input$Frequency)})


}

I expect the dashboard to show me the resultant table. But I get the error message

Warning: Error in <-: invalid (NULL) left side of assignment [No stack trace available]

1
The error is a result of trying to add new columns with myTable()$... <- . This doesn't work. See below for a simpler alternative.Eli Berkow

1 Answers

1
votes

You should be able to keep your original functions from your local script. Save them in a file global.R in the same directory as your server.R and ui.R. These will then be available to your shiny app and you can simplify your server file to:

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


    output$Results <- renderDataTable({AnalysisofReturn(input$Frequency)})


}

The global file will look something like this:

library(shiny)
library(shinydashboard)

AnalysisofReturn <- function(Freq)                                          #Function to calculate all the relevant, time point-to-time point data
{



    InputData <- as.data.frame(read.csv("Input/Dataset for R.csv",header=TRUE))

    myTable <- subset(InputData,Frequency==Freq)

    myTable$Frequency <- NULL

    myTable$Date <- as.Date(myTable$Date,format='%d/%m/%Y',origin="1970-01-01")

    myTable$ETF.Return <- as.numeric(c("-",diff(log(myTable$ETF.Index))))

    myTable$Index.Return <- as.numeric(c("-",diff(log(myTable$Underlying.Index))))

    myTable$Multiple <- myTable$ETF.Return/myTable$Index.Return
    myTable$Non.Regular.Movement <- ifelse(myTable$Multiple<0,1,0)
    myTable$Non.Regular.Positive.Movements <- ifelse(myTable$Non.Regular.Movement==1 & myTable$ETF.Return>0,1,0)
    myTable$Absolute.Deviation.From.Ideal.In.Basis.Points <- round(abs(myTable$ETF.Return-(2*myTable$Index.Return))*10000,digits=0)


}

Also, you should not need to set your working directory - as long as your data is saved within your app folder that's where it will look, hence read.csv(Input/Dataset for R.csv) above.