我的 Shiny 应用程序中有一个plotly条形图,我想在生成的条形图中为每列设置特定的颜色。
#Here's some reproducible data
df=data.frame(Month=c("Jan","Feb","Mar","Apr","May","Jun"),Criteria1=c(10,15,20,15,7,6),Criteria2=c(3,8,5,7,9,10),Criteria3=c(11,18,14,9,3,1))
#Plot
colNames <- names(df)[-1] #Month is the first column
# Here is where I set the colors for each `Criteria`, assuming that the order of colors follows the same order as the factor levels of the `Criteria`.
p <- plotly::plot_ly(marker=list(colors=c('#CC1480', '#FF9673', '#E1C8B4')))
for(trace in colNames){
p <- p %>% plotly::add_trace(data = df, x = ~Month, y = as.formula(paste0("~`", trace, "`")), name = trace, type = "bar")
}
p %>%
layout(title …Run Code Online (Sandbox Code Playgroud) 我创建了一个循环,它通过Twitter句柄向量运行,并使用包中的search_tweets函数从它们收集推文rtweet.
下载最新版本的rtweet
## install devtools package if it's not already
if (!requireNamespace("devtools", quietly = TRUE)) {
install.packages("devtools")
}
## install dev version of rtweet from github
devtools::install_github("mkearney/rtweet")
library(rtweet)
Run Code Online (Sandbox Code Playgroud)
创建令牌
## autheticate via web browser
token=create_token(
app = "My_app",
consumer_key = "My Consumer Key",
consumer_secret =
"My Secret Code",set_renv = FALSE)
Run Code Online (Sandbox Code Playgroud)
这是我的Twitter手柄,存储在矢量中
twitter_handles=c("@realDonaldTrump","@HillaryClinton","@MittRomney")
Run Code Online (Sandbox Code Playgroud)
然后我遍历这些句柄,并将每个句柄的结果存储为唯一的数据帧
#Loop through the twitter handles & store the results as individual dataframes
for(handle in twitter_handles) {
result <- search_tweets(handle, n = 3500 , …Run Code Online (Sandbox Code Playgroud) 我有两个数据集,一个包含所购买股票的详细信息buy,另一个包含所售股票的详细信息,称为sell
buy = data.frame(TransactionID = c(1:10),
Ticker=c(rep('MSFT',4),rep('AMZN',3),rep('DOCU',3)),
Date=c(rep('01-01-2020',2),rep('01-14-2020',2),rep('01-01-2020',2),rep('01-14-2020',1),'01-01-2020','03-15-2020','04-06-2020'),
Price=c(100,102,102,107,2000,2010,2011,197,182,167),
Quantity=c(10,10,5,5,1,1,2,12,15,15))
sell = data.frame(TransactionID=c(1:7),
Ticker=c('MSFT','MSFT','AMZN','AMZN','DOCU','DOCU','DOCU'),
Date=c('01-07-2020','01-20-2020','01-01-2020','01-30-2020','01-15-2020','04-10-2020','04-20-2020'),
Price=c(97,110,2100,2050,210,205,225),
Quantity=c(7,12,1,3,10,5,3))
Run Code Online (Sandbox Code Playgroud)
Date以格式输入mm-dd-YYYY
我的目标是使用 FIFO(先进先出)方法计算数据中包含的时间段内所有交易的总收益/损失。
我正在尝试使用 R 以编程方式执行此操作,但尚未成功。
方法和期望的输出
我尝试手动计算来演示 FIFO 计算方法和我达到的最终结果。我一直在寻找一种使用 R 来做到这一点的方法,但还没有找到成功的方法 -
1) 01-07-2020 - Ticker= MSFT - Sold 7 @ $97 - Total = $679
FIFO Cost of 7 @ $100 - Total = $700
Gain/Loss = -$21
2) 01-01-2020 - Ticker=AMZN - Sold 1 @ $2100 - Total = $2100
FIFO Cost of 1 …Run Code Online (Sandbox Code Playgroud) 在我的 Shiny 应用程序中,图例trace 0中的图例会产生一个使我的图表不平衡的图例。
然而,trace 0在图例中点击,图表恢复正常
有没有办法trace 0从我的情节中完全删除它?
这是我的代码:
1)我的数据框首先在reactive函数内过滤
global_evolution=reactive({
results_combined %>%
filter(!is.na(SVM_LABEL_QOL) & SVM_LABEL_QOL=='QoL' & globalsegment==input$inp_pg1segment & Account==input$inp_pg1clientsfiltered & Date >=input$inp_pg1daterange[1] & Date <=input$inp_pg1daterange[2]) %>% #Input: Account
select(Account,Date,SVM_LABEL_DIMENSION) %>%
mutate(Month=month(as.Date(format(as.POSIXct(Date),format = "%d/%m/%Y"),"%d/%m/%Y"))) %>%
select(Account,Month,SVM_LABEL_DIMENSION,-Date) %>%
group_by(Month,SVM_LABEL_DIMENSION) %>%
summarise(Monthly_Count=n()) %>%
spread(SVM_LABEL_DIMENSION,Monthly_Count) %>%
ungroup() %>%
mutate(Month=month.abb[Month]) %>%
mutate_all(funs(replace(., is.na(.), 0)))
})
Run Code Online (Sandbox Code Playgroud)
2)然后对另一个reactive函数内的过滤数据框进行更多更改
global_evolution_final=reactive({
global_evolution() %>%
mutate(Month=factor(Month,levels=c("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")))
})
Run Code Online (Sandbox Code Playgroud)
3)最后我plot_ly用来构建条形图。但是trace 0无法删除
output$pg1evolution <- …Run Code Online (Sandbox Code Playgroud) 我正在处理文本数据,并正在寻找过滤问题的解决方案。
我设法找到了一个解决方案,可以过滤包含“Word 1”或“Word 2”的行
这是可重现的代码
df=data.frame(UID=c(1,2,3,4,5),Text=c("the quick brown fox jumped over the lazy dog",
"long live the king",
"I love my dog a lot",
"Tomorrow will be a rainy day",
"Tomorrow will be a sunny day"))
#Filter for rows that contain "brown" OR "dog"
filtered_results_1=dplyr::filter(df, grepl('brown|dog', Text))
Run Code Online (Sandbox Code Playgroud)
但是,当我过滤包含“Word 1”和“Word 2”的行时,它不起作用。
#Filter for rows that contain "brown" AND "dog"
filtered_results_2=dplyr::filter(df, grepl('brown & dog', Text))
Run Code Online (Sandbox Code Playgroud)
无法找出正确的语法,任何帮助将不胜感激。
我的数据框看起来像这样 -
dataset = data.frame(ID=c(1:3),Count=c(22,NaN,13))
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用管道运算符将 NaN 替换为 0
dataset = dataset %>% replace('NaN',0)
Run Code Online (Sandbox Code Playgroud)
然而这不起作用。我查看了该网站上的解决方案,但似乎没有一个有效。
任何意见都将受到高度赞赏。
我有一个 Plotly 堆积条形图。我想更改条形的颜色,因此我有红色和深蓝色,而不是您在下面看到的默认颜色
我的代码如下。我尝试使用该marker函数,但它将整个图转换为单一颜色。任何帮助,将不胜感激。
pie_subsegment_split %>%
plot_ly(x = ~Segment, y = ~Not*100, type = 'bar', name = 'Not') %>%
add_trace(y = ~QoL*100, name = 'QoL') %>%
layout(yaxis = list(title = 'Percentage (%)'),barmode = "stack",showlegend=T) %>%
layout(xaxis = list(title="Sub-Segment",showticklabels=FALSE))
Run Code Online (Sandbox Code Playgroud)