Reputation: 19
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())
})
Edit - Data and the entire code can be found here. https://github.com/AvisR/Shiny
Upvotes: 0
Views: 136
Reputation: 1558
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)
Upvotes: 1