This blog we will learn to build Candlestick chart for stocks using R. First thing we need to ensure that “plotly” is installed from the GitHub library

install.packages(“plotly”)
install.packages(“quantmod”)

A Candlestick chart is frequently used in stocks, security, derivative or currency analysis to describe the price movement. Each candle indicates single day pattern with its open, high, low and close. Basically they look like box plot but they are not relevant to each other.

Syntax

plot_ly(x = date, open = …, high = …, low = …, close = …, type = “candlestick”)

library(plotly)
library(quantmod)

Lets take an example of Stock price movement of Infosys where green is increasing and red is decreasing movement in stock price.

getSymbols("INFY",src='yahoo')
## [1] "INFY"
# importing data into data frame and limiting to last 30 days
df <- data.frame(Date=index(INFY),coredata(INFY))
df <- tail(df,30)

Next step is to use plot_ly to plot the graph

p <- df %>%
plot_ly(x = ~Date, type="candlestick",
          open = ~INFY.Open, close = ~INFY.Close,
          high = ~INFY.High, low = ~INFY.Low) %>%
  layout(title="CandleStick Chart")

Create an sharable link for chart

# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
sharelink = api_create(p,filename="CandleStick")
## Found a grid already named: 'CandleStick Grid'. Since fileopt='overwrite', I'll try to update it
## Found a plot already named: 'CandleStick'. Since fileopt='overwrite', I'll try to update it
sharelink

Please feel free to ask any questions 🙂

Do subscribe to Tabvizexplorer.com to keep receiving regular updates.

##R markdown to WordPress
In this post we will look into steps to publish R markdown directly into wordpress. Here are the steps:

#1 Packages Needed:
Following packages needs to be installed on R studio

install.packages(“devtools”)
install.packages(“RCurl”)
install.packages(“XML”)
devtools:::install_github(“duncantl/XMLRPC”)
devtools:::install_github(“duncantl/RWordPress”)

#2 Once the packages are installed write the blog or r markdown document:
Once R markdown is completed then to upload the post on wordpress, first ensure RWordPress is loaded

library(RWordPress)

Then connect with your wordpress site with credentials:

options(WordPressLogin = c(user = 'password'), # your user name & password
        WordPressURL = 'http://tabvizexplorer/xmlrpc.php') # your WP url + /xmlrpc.php at the end

#3 Finally use knitr library to create an html code to be uploaded on wordpress

library(knitr)
knit2wp('WP_markdown.Rmd', title = 'How to Upload R Markdown Directly to WordPress', publish = FALSE) # your filename and blog post title

Done! your R markdown is ready to be publish