Stock Data Analysis – Shiny App

Folks,

Check out this awesome R Shiny App, it allows you to extract & analyze the Stock Market data using R Shiny App.

Check out below blog for the Source Code & Shiny App.

http://bigdataenthusiast.com/2016/12/14/RShinyStockApp.html

Shiny Scrrensot.JPG

Thanks!

Happy Learning! Your feedback would be appreciated!

 

Advertisements

Stock Market Analysis Using R

Stock Market Analysis Using R

Folks,

In this blog we will learn how to extract & analyze the Stock Market data using R!

Using quantmod package first we will extract the Stock data after that we will create some charts for analysis.

Quantmod – “Quantitative Financial Modeling and Trading Framework for R”!

R Package designed to assist the quantitative trader in the development, testing, and deployment of statistically based trading models. It has many features so check out its link.

Check out this blog for Quantmod getSymol R Shiny App – Link


R Packages Required:-

install.packages("quantmod")

Extracting Stock Market Data–

Functions getSymbols: It load and manage data from Multiple Sources.

getSymbols(“SYMBOL”, src=”SOURCE” , from =”YYYY-MM-DD”, to = “YYYY-MM-DD”)

Some src methods are: yahoo, google, oanda etc.

In this blog we will first extract Bombay Stock Exchange Data using yahoo finance source. Bombay Stock Exchange Index/Symbol – BSESN 

1) Analyze One Month Data of Bombay Stock Exchange- 

library(quantmod)

getSymbols("^BSESN",src="yahoo" , from ="2016-10-23", to = Sys.Date())

View(BSESN)

Here is the BSESN (xts Object) Output Data. Here you can see different columns having data for Open, High, Low, Close, Volume & Adjusted Stock Price.

High refers to the highest price of the stock touched the same day, Low refer to the lowest price the stock was traded on the same day, Close refers to the closing price of that particular stock and the Volume refer to the number of share traded that day.

1.png

Output Charts:- 

chart_Series(BSESN)

As you can see in below chart there was huge dip after 8 Nov 2016, may be this is due to demonetization in India.

3.JPG

2) Analyze One Year Data of Bombay Stock Exchange- 

getSymbols("^BSESN",src="yahoo" , from ="2015-10-23", to = Sys.Date())

chart_Series(BSESN,type = "candlesticks")

Output Chart:-

4.JPG

2) Complete Data of Bombay Stock Exchange– 

It will provide you all data after 2007.

getSymbols("^BSESN",src="yahoo")

Quantmod has some other features. For more details, please visit this Link.


Thanks!

Happy Learning! Your feedback would be appreciated!

Interactive Maps in R: Leaflet

Interactive Maps in R: Leaflet

Folks,

Leaflet is one of the most popular open-source JavaScript libraries for interactive maps.

There are many ways to visualize latitude and longitude data on map using R, such as using ggmaps or RgoogleMaps packages. But these packages generate static maps images only. Leaflet allow users to zoom in or zoom out in a very interactive  way.

In this blog we will learn how to create a interactive map using Leaflet in R & also we will learn how to map and style – latitude and longitude data using R & Leaflet package!

Basics of Leaflet in R – bigdataenthusiast.com/2016/12/12/Leaflet.html


R Packages Required:  

install.packages("leaflet")

1) Creating a map using leaflet –

R Code – 

Code 0.JPG

Here leaflet() initializes the leaflet workspace & addTiles() will bring default OpenStreetMap tiles. OpenStreetMap is a free open-source service to create a free editable map of the world.

Output –

R Viewer (Snapshot)- It allow users to zoom in or zoom out in a very interactive way.

out.JPG

I have published this output on RPubs. Click on below link to see interactive output!

Output Link – rpubs.com/BdataEnthusiast/InteractiveMap

2) Creating a map with single marker –

Suppose user want to mark 28.61293° N, 72.229564° E “India Gate” co-ordinates on Map.

R Code – 

code-1

Here addMarkers – Add markers to the map eg. geo co-ordinates, Popup, link. etc.

Output –

I have published this output on RPubs. Click on below link to see interactive output!

Link – rpubs.com/BdataEnthusiast/InteractiveMap01

Output GIF –

ezgif.com-crop.gif

2) Creating a map with multiple marker –

Suppose user want to mark multiple co-ordinates on Map.

Eg. – Below is R dataframe (india_smart_cities) having  latitude & longitude of 30 proposed smart cities in India.

data.jpg

R Code – 

code multiple.JPG

Output– Click on below link to see interactive output!

Link – rpubs.com/BdataEnthusiast/IndiaSmartCities


If you also share your interactive map outside of the RStudio environment, just click on the Save as Web Page option in Export. It will generate an HTML file.

2


 

Check out this awesome leaflet R Shiny App Blog. This basic R Shiny App allows you to locate your geographic coordinates on Leaflet interactive map.

Check out the Leaflet R Shiny App here shinyapps.io/LeafletShinyR/

leafletshinyr

For more details of R leaflet package, please visit this Link.


Thanks!

Happy Learning! Your feedback would be appreciated!

Face API in R – Microsoft Cognitive Services

Face API in R – Microsoft Cognitive Services

Folks,

In this blog we will explore Face API in R (Face API – Microsoft Cognitive Services)

This API can detect human faces in image and returns face locations, landmarks, and other important attributes like (age, gender, smile & glasses etc.)

Click here & Register for the free subscription of Microsoft Cognitive Services (Face API).

Here is my free subscription. Free 30,000 transactions per month. 20 per Minute.

1.JPG

After registering please copy the Subscription Key. This is the Subscription Key which provides access to this API.


Face – Detect API

Request URL:

https://api.projectoxford.ai/face/v1.0/detect[?returnFaceId][&returnFaceLandmarks][&returnFaceAttributes]

Request Parameters:

Parameter 1: returnFaceId (Optional) 
Type: Boolean(Default value is true)
Decription: It return unique faceIds of the detected human face in the image. 

Parameter 2: returnFaceLandmarks (Optional)
Type: Boolean(Default value is false)
Description: It return face landmarks of the detected human face in the image..

Parameter: returnFaceAttributes (Optional) 
Type: String (Comma Seperated) 
Description: It return the face attributes including age, gender, headPose, smile, facialHair & glasses.
Input Example: "returnFaceAttributes=age,gender"

Request Headers:

Content-Type (optional): Media type of the body sent to the API.
application/json or application/octet-stream
Ocp-Apim-Subscription-Key: Subscription key which provides access to this API.

Request Body: JSON or Binary Data

{
    "url":"Url of the image"
}

R Commands & Output:-

R Packages Required: httr

library(httr)

# Below is the URL having returnFaceLandmarks = true and returnFaceAttributes = age,gender,headPose,smile,facialHair,glasses

face_api_url = "https://api.projectoxford.ai/face/v1.0/detect?returnFaceLandmarks=true&returnFaceAttributes=age,gender,headPose,smile,facialHair,glasses"

# Below is the image we are going to upload

body_image = upload_file("C:\\Users\\lenovo\\Documents\\image.jpeg")

# Below is the POST methord (Adding Request headers using add_headers)

result = POST(face_api_url,
              body = body_image,
              add_headers(.headers = c("Content-Type"="application/octet-stream",
                                       "Ocp-Apim-Subscription-Key"="Subscription Key")))

API_Output = content(result)

# Coverting Output into R Dataframe 

Output_Face_Attributes = as.data.frame(API_Output)

Input:-

input

Input R Code Snapshot: 

2.png

Output:-

View(Output_Face_Attributes)

Here you can check the attributes of the image.

3.png

For more details & other Image and Face API’s, please visit this Link.


Thanks!

Happy Learning! Your feedback would be appreciated!

Scheduling R Script using Windows Task Scheduler

Scheduling R Script using Windows Task Scheduler

Folks,

In this blog we will learn how to schedule R Script using Windows Task Scheduler!


Suppose here is the R Script which we want to schedule.

Output of this script: Create a log file of name “R_Scripts_Logs_<Time stamp>.TXT” & having text “Script successfully invoked by scheduler at <Time stamp>” .

1

Create a batch file having below commands:

@echo off
“Location of R.exe” CMD BATCH “Location of R Script”

Example: 

2.png


Steps for scheduling R Script using Windows Task Scheduler:

Open Window Task Scheduler & Create Basic Task

3

Step 1: Provide your task name & description.step-1

Step 2: Provide trigger timings details as per your requirement.

step-2

step-3

Step 3: In Action:- provide the location of the batch file of the script. After that save the task.

step-5step-6

Task has been scheduled.

step8

At 22:05, task triggered the batch file automatically.

Here is the output of that script:

outut

When you run R script using CMD Batch, one ROUT file also generated automatically having details of script commands & time elapsed of script.

Below is ROUT fie of the script my R Script.R

output


Thanks!

Happy Learning! Your feedback would be appreciated!

Geocoding in R!

Folks,

In this blog we will explore the basics of Geocoding using R,  using Google Maps Geocoding API & R!

Geocoding: Process of transforming a description of a location (address, name of a place, or coordinates) to a location on the earth’s surface.

Forward Geocoding: Converting address/name of a place into geographic coordinates.

Reverse Geocoding: Converting geographic coordinates into readable address.

R Package required: 

install.packages(“RCurl ”): It allows us to compose general HTTP requests and provides convenient functions to fetch data.

install.packages(“rjson”): It allows us to converts JSON object into R objects and vice-versa.


Forward Geocoding using R

Google Maps Geocoding API:

11

Where output may be either of JSON/XML & parameters = address or components.

Example for JSON:

https://maps.googleapis.com/maps/api/geocode/json?address=India+Gate,+New+Delhi,+India&components=country:IN

Where address = “India Gate, New Delhi, India”, components = Country “IN” for India.

Component Country: country name or a two letter country code. For more components like locality & postal code etc. details read this link.

R Commands:

url<- “”

jdata<- getURL(url) # Fetching JSON data.

rdata<- fromJSON(jdata) # Convert a JSON object into an R object.

Output:

1

Geographic coordinates present in  rdata results.

2

Extracting geographic coordinates from the rdata.

3.png

PS: These are approx geographic coordinates, as it depends on the address provided in URL.


Reverse Geocoding using R

Google Maps Reverse Geocoding API:

12

Where output may be either of JSON/XML. Parameters:  ,

R Commands:

url<- “”

jdata<- getURL(url) # Fetching JSON data.

rdata<- fromJSON(jdata) # Convert a JSON object into an R object.

For Example taking latitude & longitude 28.632455, 77.217218 – Courtesy: Google Maps

4.png

Output:

7.png

5

For more details please read this google link.


Thanks!

Happy Learning!

Accessing Database (MS SQL Server/Oracle/MySQL) from R!

Folks,

In this blog we will learn how to access Oracle, MySQL & SQL Server databases from R!


Accessing Oracle Database using RJDBC Package

Below is my database hosted on AWS Cloud. See here how to setup Oracle Db on cloud (free tier).

11

R Packages required:-  install.packages(“RJDBC”)

Also download Oracle JDBC Drivers from here Link. After downloading, copy file location.

location.png

Commands:


driver <- JDBC("oracle.jdbc.driver.OracleDriver"," <paste JDBC Driver file location>")

dbConnect(driver , "jdbc:oracle:thin:@Host:Port:ServiceName or SID", "<username>", "<password>")

code1

R users have few more choices to access their Oracle Database: Using RODBC and ROracle packages.


MySQL Database

Here is my database hosted on AWS Cloud. See here how to setup MySQL Db on cloud (free tier).

1

a) Accessing MySQl Database using ODBC:

R Packages required:-  install.packages(“RODBC”)

For Windows: Go to start, search ODBC. Follow below steps of setup.

1

Click on “Add”.

2

Select MySQL ODBC Driver. If not present, then download using this link.

3

Provide your server details, username & password. Test your connection. Note down Data Source name.

4

Commands:


my_conn <- odbcConnect("<ODBC Data Source Name >")

sqlQuery(my_conn, paste("<Query>"))

code

b) Accessing MySQL DB using RMySQL Package:

R Packages required:-  install.packages(“RMySQL”)

Commands:


my_conn <- dbConnect(MySQL(), user="<username>", password="<password>", database= "<Database name>", host="<your host>")

result<- dbSendQuery(my_conn, "<Query>")

dbFetch(result)

code 2.png


Accessing SQL Server Database using ODBC

Here is my SQL Server database hosted locally.

server.png

a) Accessing SQL Server using ODBC:

For Windows: Go to start, search ODBC.

1

Follow below steps to setup ODBC datasource.

output_KUL4JC.gif

Commands:


my_conn <- odbcConnect("<Data Source Name>", uid="<username>", pwd="<password>")

sqlQuery(my_conn, "<Query>")

code2.png

b) Accessing SQL Server DB using RJDBC Package:

Suppose SQL Server database hosted on cloud. We can use below commands to connect.

R Packages required:-  install.packages(“RJDBC”) & Download SQL JDBC Drivers file from internet.


driver <- JDBC("com.microsoft.sqlserver.jdbc.SQLServerDriver",
"<location of SQL JDBC Drivers file on computer>")

conn <- dbConnect(driver , "jdbc:sqlserver://<serverName>", "<userID>", "<password>")

result <- dbGetQuery(conn, "<Query>")


Thanks!

Happy Learning!