0

This is my first Shiny app.

I am trying to build a dashboard that will find stocks that cointegrated and forecast price using them.

Note - I understand the page number really doesn't matter in the code and I'm only using it as reference below.

The flow of the dashboard is the following -

Page 1 - Select Stock (eg. Stock "A") -> Plot -> Structural Break -> Unit Root -> Find Cointegrating Pairs

I have no issue in page 1.

Page 2 -> Select stock that are cointegrated with stock A (drop down list populated by the Cointegrated Pairs from Page 1. Should allow of 0 objects too since it is possible for the stock to not be cointegrated with any stock)

Problem - How do I retrieve the list of cointegrated pairs from the page 1 and use it as the input for Page 2's drop down menu?

Below is code to create the list of Cointegrated Pairs. I use renderPrint to show the output in Page 1.

Coin <- reactive({
    req(input$Stock_Sym)
    Sym <- input$Stock_Sym
    remove(Row)
    remove(Col)
    if (Sym %in% Stseries$Stseries) {
        print("The Stock is stationary and cannot have cointegrating relationship")
    } else if (Sym %in% Useries$Useries) {
        Row <- as.data.frame(Jou[,Sym])
        rownames(Row) <- rownames(Jou)
        Col <- as.data.frame(Jou[Sym,])
        Col <- as.data.frame(t(Col))
        rownames(Col) <- colnames(Jou)
        CopairsR <- rownames(Row)[Row[,1]=="Yes"]
        CopairsC <- rownames(Col)[Col[,1]=="Yes"]
        CopairsU <- c(unique(CopairsR, CopairsC))
        CopairsU <- ifelse(length(CopairsU)==0, print("The Stock is not cointegrated with any other stock"), print(CopairsU))
    } else if (Sym %in% Eseries$Eseries) {
        Row <- as.data.frame(Joe[,Sym])
        rownames(Row) <- rownames(Joe)
        Col <- as.data.frame(Joe[Sym,])
        Col <- as.data.frame(t(Col))
        rownames(Col) <- colnames(Joe)
        CopairsR <- rownames(Row)[Row[,1]=="Yes"]
        CopairsC <- rownames(Col)[Col[,1]=="Yes"]
        CopairsE <- c(CopairsR, CopairsC)
        CopairsE <- unique(CopairsE)
        CopairsE <- ifelse(length(CopairsE)==0, print("The Stock is not cointegrated with any other stock"), print(CopairsE))
    }   else {
        print("The stock either do not have enough Observation or is not I(1)")
    }
})

output$`Possible Pairs` <- renderPrint({
    Coin <- Coin()
})

Below is the code for Page 2 Drop down menu - This doesn't work. It only gives me the one value, which is the first value as you can see in the case of stock "A".

#Input - Pair Selection

    observe({
    updateSelectInput(session, "Pair_Sym" , choices = Coin())
})

Page 1 Page 2

Edit - Data and the entire code can be found here. https://github.com/AvisR/Shiny

SivaR
  • 19
  • 5
  • Hi, welcome to SO ! you should provide a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) so we can test what's wrong in your code. I tried the code hosted on your GitHub link but I get some errors and was not able to launch the app. – julien.leroux5 Sep 07 '21 at 13:43
  • My bad. I thought code would work. Could you tell me what the error was? In the mean time, let me figure a way to create the MRE. – SivaR Sep 07 '21 at 13:49
  • I got an "undefined selected columns" error when creating the `Breakpoints` in the Global Environment - structural breaks part. – julien.leroux5 Sep 07 '21 at 14:00
  • I was not able to fix the error you got since I couldn't replicate it. But I have created all the required datasets and codes to make it work. MRE can be found [here](https://github.com/AvisR/AvisR/tree/MRE). – SivaR Sep 07 '21 at 14:47

1 Answers1

1

This is because CopairsE <- unique(CopairsE) returns a character vector with quotes. You can verify the type of your objects by debugging your Shiny app.

Try to use the noquote() function and return the result to the Coin() reactive:

        CopairsE <- unique(CopairsE)
        vars <- noquote(CopairsE)
        CopairsE <- ifelse(length(CopairsE)==0, print("The Stock is not cointegrated with any other stock"), print(CopairsE))
        return(vars)
julien.leroux5
  • 969
  • 7
  • 17
  • Hi Julien, I'm still getting the same result. I removed the quotes and the results in my first page is without quotes but the dropdown is still stuck with only one value. . – SivaR Sep 07 '21 at 17:23
  • I was able to make it work with your simplified version. Try with the APPL stock symbol. To make it work with the A symbol, use the same principle with `CopairsU`. – julien.leroux5 Sep 07 '21 at 17:33
  • I have a weird issue. When I run the ifelse function, it removes all except the first element. (For eg. Incase of stock "A", instead of result being "BIIB" "CI" "DLTR" "DUK" "EIX" "IBM" "IFF" "J" "KO" "LEG" "MNST" "PNR" "PPG" "SRE" "SWK" "ZBH", it changes to "BIIB" – SivaR Sep 07 '21 at 17:49
  • And if I remove the ifelse function, it works. Thank you so much. Is there a reason why the ifelse function seem to change CopairsE or CopairsU to just the first element of the list? (While printing the entire list in Page 1) – SivaR Sep 07 '21 at 17:55
  • Do you really return `vars` as in my example? Be careful: if you return `CopairsE`, it will behave as you said because of the `print(CopairsE)` statement. – julien.leroux5 Sep 07 '21 at 18:14
  • Yeah, I didn't do that. It works now. That was my mistake. Thank you so much Julien. That was super helpful. I'm not able to upvote since I don't have enough reputation but I'll do once I have. I owe you one. Thanks again! – SivaR Sep 07 '21 at 18:20
  • Good to hear, glad it was helpful! – julien.leroux5 Sep 07 '21 at 19:04