r - R:“order.by”中的索引条目不是唯一的(动物园)
问题描述
我正在提取财务数据并尝试以闪亮的方式输出它。我不熟悉动物园功能。为了使正回报为绿色,负回报为红色,我收到以下错误:
zoo(cd, order.by = index(x), ...) 中的警告:如果 'order.by' 中的索引条目不是唯一的,则“zoo”对象的某些方法不起作用
library(tidyverse)
library(tidyquant)
library(shiny)
#Stock assignments
stock_1<-'AMZN'
stock_2<-'CMG'
stock_3<-'TSS'
stock_4<-'FB'
stock_5<-'T'
#Creating Input Boxes for Stocks and Weights of the Portfolio
ui <- fluidPage(
titlePanel('Portfolio Daily Returns'),
sidebarLayout(
sidebarPanel(
fluidRow(
column(6,textInput("S1",h3('Input Stock 1'),stock_1)),
column(6,numericInput("w1",h3("Input Weight 1"),.25))
),
fluidRow(
column(6,textInput("S2",h3('Input Stock 2'),stock_2)),
column(6,numericInput("w2",h3("Input Weight 2"),.25))
),
fluidRow(
column(6,textInput("S3",h3('Input Stock 3'),stock_3)),
column(6,numericInput("w3",h3("Input Weight 3"),.2))
),
fluidRow(
column(6,textInput("S4",h3('Input Stock 4'),stock_4)),
column(6,numericInput("w4",h3("Input Weight 4"),.2))
),
fluidRow(
column(6,textInput("S5",h3('Input Stock 5'),stock_5)),
column(6,numericInput("w5",h3("Input Weight 5"),.1))
),
dateRangeInput("D",h3("Input Start and End Dates"),
'2019-09-01',Sys.Date())
),
mainPanel(plotOutput("plot"))
)
)
#Shows daily volatility of your portfolio returns. Set for the last month but can adjust
server <- function(input,output){
dataInput <- reactive({
dat <- tq_get(c(input$S1,input$S2,input$S3,input$S4,input$S5),from=input$D[1],to=input$D[2])%>%
group_by(symbol)%>%
tq_transmute(select=volume,mutate_fun = periodReturn,period='daily',
col_rename = 'Daily_Vol')%>%
tq_portfolio(assets_col=symbol,returns_col = Daily_Vol,weights=c(
input$w1,input$w2,input$w3,input$w4,input$w5),col_rename = 'Port_Vol')
dat$sign = ifelse(dat$Port_Vol >= 0, "positive", "negative")
return(dat)
})
output$plot <- renderPlot({
ggplot(dataInput(),aes(x=date,y=Port_Vol))+geom_col()
scale_fill_manual(values = c("positive"="green","negative"="red"))
})
}
shinyApp(ui=ui,server=server)
解决方案
Here's a solution to get a graph with red and green bars. First change the dat$sign assignment statement to match the following:
dat$sign = ifelse(dat$Port_Vol >= 0, TRUE, FALSE)
Then change the renderPlot statement to match the following:
output$plot <- renderPlot({
dat <- dataInput()
ggplot(dat,aes(x=date,y=Port_Vol,fill=sign))+geom_col()+
scale_fill_manual(values = c("red","green"))
})
I did not get a 'zoo' error when running the code, so I cannot help you with that issue. You may want to make sure the zoo library is updated on your machine.
推荐阅读
- linux - GREP 或 Regex 搜索具有特定系列的唯一字符
- javascript - 如何更改饼图中向下钻取的点格式?
- c++ - 如何在 WinDBG 预览版上设置源服务器?
- visual-studio - 解决方案资源管理器:将搜索限制为选定的文件夹
- facebook - Xamarin 和 Facebook 身份验证
- c# - 通过调度程序/数据绑定在此更新 UI 中缺少什么
- machine-learning - 给定来自不同广告商的描述,生成不特定于任何一个广告商的一般描述?
- elasticsearch - 如何在 Kibana elasticSearch 中搜索 Json 消息
- odata - SAPUI5 OData 401(未经授权)+无访问控制允许来源标题
- sql-server - 尝试在 Azure SQL 上添加多个用户