Black Lives Matter. Please consider donating to Black Girls Code today.
Dash Enterprise delivers an incredible 21x cost savings 💸Download the e-book!

Candlestick charts using quandl and plotly example not working (OS X and Ubuntu)

hi! I’ve tried http://moderndata.plot.ly/candlestick-charts-using-quandl-and-plotly/ in RStudio on my Mac and on remote Ubuntu box and it failed in both cases. Steps to reproduce:

On Mac:

R version 3.3.1 (2016-06-21) – “Bug in Your Hair”
Copyright © 2016 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin13.4.0 (64-bit)

RStudio: Version 0.99.903
Quandl: 2.8.0

When run the example
Error: could not find function “%>%”

Then after adding obviously missing line (which was the reason of %>% operator errror):

library(plotly)

It gave me a warning that “layout” object is now “masked”:

Warning message:
‘layout’ objects don’t have these attributes: ‘color’
Valid attributes include:
‘font’, ‘title’, ‘titlefont’, ‘autosize’, ‘width’, ‘height’, ‘margin’, ‘paper_bgcolor’, ‘plot_bgcolor’, ‘separators’, ‘hidesources’, ‘smith’, ‘showlegend’, ‘dragmode’, ‘hovermode’, ‘xaxis’, ‘yaxis’, ‘scene’, ‘geo’, ‘legend’, ‘annotations’, ‘shapes’, ‘images’, ‘updatemenus’, ‘ternary’, ‘mapbox’, ‘radialaxis’, ‘angularaxis’, ‘direction’, ‘orientation’, ‘barmode’, ‘bargap’, ‘mapType’

so something is already messed up, but lets try to comment the line which tries to set ‘color’ attribute (line 26):

and try to run it again:
[can not add more screenshots - new users limit: 3 screenshots, 2 links]
this time it finishes with no warnings / errors / any charts

please help to run your example.

Also: “this older post” mentioned in the article and available by this link:
http://moderndata.plot.ly/candlestick-charts-using-plotly-and-quantmod/
also does not work. The error I get is:

library(plotly)
library(quantmod)
Loading required package: TTR
Version 0.4-0 included new data defaults. See ?getSymbols.
plotlyCandleStick(“TSLA”)
Error in plot_ly(plot.base, x = x, y = y, mode = “lines”, marker = list(color = “#9b9797”), :
object ‘y’ not found

this is I guess has something to do with known issue reported to plotly:
[basic plotting naming convention (non-tilde ~) broken?

I tried to fix it, but then there were other errors.

Please help with running your quantmod and quantdl candlesticks charts examples - they look awesome on the screenshots!

cheers,
Dmitry

Seems to work for me. Yes the color = "white" argument is not needed anymore. But the chart populates fine. Which version of plotly are you using?

Ignore the older post. It has bugs.

I’ve replied by email 2 hours ago, but don’t see my response on this thread yet, so I’ll copy-paste my email response here (via browser / forum) (are my replies moderated and I just need to be a bit more patient OR my email response was just silently dropped?)

thank you for quick response!
My RStudio claims (on 'Packages' tab) it uses: plotly 4.5.2

Could you try with the more recent version on github? 4.5.5?

Short answer:
yes, your example works fine with plotly 4.5.5 installed from GitHub.
The “installed by default” plotly v. 4.5.2 does not work.
Thanks!
Dmitry


A bit longer answer:

# just reinstalling plotly to double check which version I would end up with (answer: still brings plotly v. 4.5.2):

remove.packages(“plotly”)
Removing package from ‘/Library/Frameworks/R.framework/Versions/3.3/Resources/library’
(as ‘lib’ is unspecified)
install.packages(“plotly”)
trying URL ‘https://cran.rstudio.com/bin/macosx/mavericks/contrib/3.3/plotly_4.5.2.tgz
Content type ‘application/x-gzip’ length 806762 bytes (787 KB)
==================================================
downloaded 787 KB

# Installing plotly from github

uninstall plottly

remove.packages(“plotly”)

install package “devtools”, so we can install latest plotly from github

install.packages(“devtools”)
library(devtools)

install plotly package from github

install_github(“ropensci/plotly”)

surprisingly installation went well, but no “plotly” package listed under “Packages” tab:

# but RStudio seems able to load (and autocomplete works for package name plotly):

library(plotly)
Loading required package: ggplot2
Attaching package: ‘plotly’
The following object is masked from ‘package:ggplot2’:
last_plot
The following object is masked from ‘package:stats’:
filter
The following object is masked from ‘package:graphics’:
layout

# and even though not listed among “Packages” tab, we can see it is actually loaded:

(.packages())
[1] “Quandl” “xts” “zoo” “plotly” “ggplot2” “devtools”
[7] “stats” “graphics” “grDevices” “utils” “datasets” “methods”
[13] “base”

# funny that the installation and loading the plotly from github didn’t mention the version anywhere!

install_github(“ropensci/plotly”)
Downloading GitHub repo ropensci/plotly@master
from URL h_t_tps://api.github.com/repos/ropensci/plotly/zipball/master
Installing plotly
Downloading GitHub repo hadley/ggplot2@master
from URL h_t_tps://api.github.com/repos/hadley/ggplot2/zipball/master
Installing ggplot2
trying URL ‘h_t_tps://cran.rstudio.com/bin/macosx/mavericks/contrib/3.3/reshape2_1.4.2.tgz’
Content type ‘application/x-gzip’ length 202343 bytes (197 KB)
==================================================
downloaded 197 KB

Installing reshape2
‘/Library/Frameworks/R.framework/Resources/bin/R’ --no-site-file
–no-environ --no-save --no-restore --quiet CMD INSTALL
‘/private/var/folders/jc/2zmsbd7j1cq70jh78dknch6m0000gs/T/RtmpbBRQ88/devtools42ab445b4dd7/reshape2’
–library=’/Library/Frameworks/R.framework/Versions/3.3/Resources/library’
–install-tests

  • installing binary package ‘reshape2’ …

  • DONE (reshape2)
    Downloading GitHub repo hadley/scales@master
    from URL h_t_tps://api.github.com/repos/hadley/scales/zipball/master
    Installing scales
    ‘/Library/Frameworks/R.framework/Resources/bin/R’ --no-site-file
    –no-environ --no-save --no-restore --quiet CMD INSTALL
    ‘/private/var/folders/jc/2zmsbd7j1cq70jh78dknch6m0000gs/T/RtmpbBRQ88/devtools42ab2194467c/hadley-scales-d58d83a’
    –library=’/Library/Frameworks/R.framework/Versions/3.3/Resources/library’
    –install-tests

  • installing source package ‘scales’ …
    ** libs
    clang++ -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I/usr/local/include -I/usr/local/include/freetype2 -I/opt/X11/include -I"/Library/Frameworks/R.framework/Versions/3.3/Resources/library/Rcpp/include" -fPIC -Wall -mtune=core2 -g -O2 -c RcppExports.cpp -o RcppExports.o
    clang++ -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I/usr/local/include -I/usr/local/include/freetype2 -I/opt/X11/include -I"/Library/Frameworks/R.framework/Versions/3.3/Resources/library/Rcpp/include" -fPIC -Wall -mtune=core2 -g -O2 -c colors.cpp -o colors.o
    clang++ -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/Library/Frameworks/R.framework/Resources/lib -L/usr/local/lib -o scales.so RcppExports.o colors.o -F/Library/Frameworks/R.framework/… -framework R -Wl,-framework -Wl,CoreFoundation
    installing to /Library/Frameworks/R.framework/Versions/3.3/Resources/library/scales/libs
    ** R
    ** tests
    ** preparing package for lazy loading
    ** help
    Loading required package: scales
    *** installing help indices
    ** building package indices
    ** testing if installed package can be loaded

  • DONE (scales)
    ‘/Library/Frameworks/R.framework/Resources/bin/R’ --no-site-file
    –no-environ --no-save --no-restore --quiet CMD INSTALL
    ‘/private/var/folders/jc/2zmsbd7j1cq70jh78dknch6m0000gs/T/RtmpbBRQ88/devtools42ab614edf51/hadley-ggplot2-cd6a691’
    –library=’/Library/Frameworks/R.framework/Versions/3.3/Resources/library’
    –install-tests

  • installing source package ‘ggplot2’ …
    ** R
    ** data
    *** moving datasets to lazyload DB
    ** inst
    ** tests
    ** preparing package for lazy loading
    ** help
    *** installing help indices
    ** building package indices
    ** installing vignettes
    ** testing if installed package can be loaded

  • DONE (ggplot2)
    ‘/Library/Frameworks/R.framework/Resources/bin/R’ --no-site-file
    –no-environ --no-save --no-restore --quiet CMD INSTALL
    ‘/private/var/folders/jc/2zmsbd7j1cq70jh78dknch6m0000gs/T/RtmpbBRQ88/devtools42ab7b44c95c/ropensci-plotly-32020c2’
    –library=’/Library/Frameworks/R.framework/Versions/3.3/Resources/library’
    –install-tests

  • installing source package ‘plotly’ …
    ** R
    ** data
    *** moving datasets to lazyload DB
    ** inst
    ** tests
    ** preparing package for lazy loading
    ** help
    *** installing help indices
    ** building package indices
    ** testing if installed package can be loaded

  • DONE (plotly)

library(plotly)
Loading required package: ggplot2

Attaching package: ‘plotly’
The following object is masked from ‘package:ggplot2’:
last_plot
The following object is masked from ‘package:stats’:
filter
The following object is masked from ‘package:graphics’:
layout

# the only way (I found) to see the version of loaded plotly (which happen to be 4.5.5.9000):

sessionInfo()
attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] Quandl_2.8.0 xts_0.9-7 zoo_1.7-13
[4] plotly_4.5.5.9000 ggplot2_2.1.0.9001 devtools_1.12.0

loaded via a namespace (and not attached):
[1] Rcpp_0.12.7 knitr_1.14 magrittr_1.5 munsell_0.4.3
[5] lattice_0.20-33 viridisLite_0.1.3 colorspace_1.2-7 R6_2.2.0
[9] httr_1.2.1 plyr_1.8.4 dplyr_0.5.0 tools_3.3.1
[13] grid_3.3.1 gtable_0.2.0 DBI_0.5-1 git2r_0.15.0
[17] withr_1.0.2 htmltools_0.3.5 yaml_2.1.13 lazyeval_0.2.0
[21] digest_0.6.10 assertthat_0.1 tibble_1.2 tidyr_0.6.0
[25] purrr_0.2.2 base64enc_0.1-3 htmlwidgets_0.7 curl_2.1
[29] memoise_1.0.0 scales_0.4.0.9003 jsonlite_1.1

trying your example http://moderndata.plot.ly/candlestick-charts-using-quandl-and-plotly/

(I’ll paste code here for completeness)

library(Quandl)
library(plotly)
df <- Quandl(“WIKI/AAPL”)
df <- df[,c(1, 9:12)]

names(df) <- c(“Date”, “Open”, “High”, “Low”, “Close”)
df$Date <- as.Date(df$Date)

df <- df[1:1000,]

hovertxt <- Map(function(x, y)paste0(x, “:”, y), names(df), df)
hovertxt <- Reduce(function(x, y)paste0(x, “<br>”, y), hovertxt)

plot_ly(df, x = ~Date, xend = ~Date, hoverinfo = “none”,
color = ~Close > Open, colors = c("#00b386","#ff6666")) %>%

add_segments(y = ~Low, yend = ~High, line = list(width = 1, color = “black”)) %>%

add_segments(y = ~Open, yend = ~Close, line = list(width = 3)) %>%

add_markers(y = ~(Low + High)/2, hoverinfo = “text”,
text = hovertxt, marker = list(color = “transparent”)) %>%

layout(showlegend = FALSE,
yaxis = list(title = “Price”, domain = c(0, 0.9)),
annotations = list(
list(xref = “paper”, yref = “paper”,
x = 0, y = 1, showarrow = F,
xanchor = “left”, yanchor = “top”,
align = “left”,
text = paste0(“AAPL”)),

       list(xref = "paper", yref = "paper", 
            x = 0.75, y = 1, showarrow = F, 
            xanchor = "left", yanchor = "top",
            align = "left",
            text = paste(range(df$Date), collapse = " : "),
            font = list(size = 8))),
     plot_bgcolor = "#f2f2f2")

# worked this time!

Thanks!

thank you for quick response!
My RStudio claims (on ‘Packages’ tab) it uses: plotly 4.5.2

Great ! good to know the example worked. You can also use packageVersion("plotly") to check the version of a specific package.