【发布时间】:2021-11-04 07:34:09
【问题描述】:
这是我的第一个闪亮的应用程序。
我正在尝试构建一个仪表板,用于查找协整的股票并使用它们预测价格。
注意 - 我知道页码在代码中并不重要,我只是将其用作下面的参考。
仪表板的流程如下-
第 1 页 - 选择股票(例如股票“A”) -> 绘图 -> 结构中断 -> 单位根 -> 查找协整对
我在第 1 页没有问题。
第 2 页 -> 选择与股票 A 协整的股票(由第 1 页的协整对填充的下拉列表。也应允许 0 个对象,因为股票可能不与任何股票协整)
问题 - 如何从第 1 页检索协整对列表并将其用作第 2 页下拉菜单的输入?
以下是创建协整对列表的代码。我使用 renderPrint 在第 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()
})
下面是第 2 页下拉菜单的代码 - 这不起作用。它只给了我一个值,这是您在股票“A”的情况下看到的第一个值。
#Input - Pair Selection
observe({
updateSelectInput(session, "Pair_Sym" , choices = Coin())
})
编辑 - 数据和整个代码可以在这里找到。 https://github.com/AvisR/Shiny
【问题讨论】:
-
嗨,欢迎来到 SO !你应该提供一个minimal reproducible example,这样我们就可以测试你的代码有什么问题。我尝试了托管在您的 GitHub 链接上的代码,但出现了一些错误,无法启动应用程序。
-
我的错。我认为代码会起作用。你能告诉我错误是什么吗?同时,让我想办法创建 MRE。
-
在全球环境中创建
Breakpoints时出现“未定义的选定列”错误 - 结构中断部分。 -
我无法修复您遇到的错误,因为我无法复制它。但我已经创建了所有必需的数据集和代码以使其工作。 MRE 可以找到here。
标签: r shiny reactive-programming quantitative-finance