Friday, June 10, 2016

About-R_ConnectToSqlServer

This post will demonstrate how to connect R to Microsoft SQL Server, so that data can be extracted directly from a database by using SQL-statements. The approach described in this post is supported by both SQL Server 2012 and the upcoming SQL Server “14”. You can connect to SQL Server using different techniques – one of them is by using ODBC. This post will use ODBC.
The first time you need to connect to a database, you need to perform some one-time tasks, which are:
  • Create a ODBC DSN data source
  • Install necessary R-packages from CRAN
The screenshot below shows a table containing the well-known data set ‘weather.nomnial’. The table is part of a database named ‘MiningDataSets’. The goal of this tutorial is to load all this data into R.
SQLR00
Create DSN
First we need to setup a user DSN data source pointing at our SQL Server using ODBC. The data source will be called from R using the package “RODBC”
  1. Open “Administrative Tools” and “ODBC Data Sources (32 bit)”
WEKASQL02
  1. On the tab “User DSN” press “Add”
  2. Select “SQL Server” in the provider list
  3. Now give the data source a name and a description. Remember this name – you will need it later on in the process. Last provide the name of the server where the SQL Server is installed. Press “Next”.
WEKASQL03
  1. Select the way in which you will authenticate against the SQL Server. In this case we use Integrated Security. Press “Next”.
WEKASQL04
  1. You now have the possibility to select the default database for the data source. Here we choose to point at the ‘MiningDataSets’. Press “Next” and the “Finish”.
WEKASQL05
  1. On the last page remember to press the “Test Connection” button and ensure that the connection could be established.
  2. The User DSN is now created and active.
WEKASQL06

Install and load RODBC

Support for SQL Server is not possible using native R – therefore we have to install the RODBC package from CRAN.
  1. Open up R and in the console window type: install.packages(“RODBC”)
    SQLR01
  2. The RODBC packages is now downloaded and installed on your system. Next step is to load the package into R so the functions of the package can be used. In the console window type: library(“RODBC”)
    SQLR02
  3. The RODBC package is now loaded and ready
Connect with R
Now it is time to connect to the SQL Server database from R and retrieve the nominal weather dataset.
  1. When calling the database you first have to open a connection to the database. And after you have performed you operations, you have to close the connection again. This is done by using the commands odbcConnect() and odbcClose(). The name specified as the parameter is the name of the ODBC user data source.
    SQLR03
  2. First we want to read an entire table into R – this can be done by using the sqlFetch command.
    SQLR04
  3. If you want to load the data into a data.frame, this can easily be done
    SQLR05
  4. If you want to execute a specific SQL-query, this can be done using the sqlQuery-command.
    SQLR06
You can use many more SQL-statements against the database by using different RODBC-functions. You can get more help about the RODBC-package by typing RShowDoc(“RODBC”, package=”RODBC”)
SQLR07
I hope this little tutorial helped you in connecting R to SQL Server
SOURCE https://andersspur.wordpress.com/2013/11/26/connect-r-to-sql-server-2012-and-14/

Thursday, June 9, 2016

About-R_DataTypeConversionToSqlServer

The following table shows the changes in data types and values when data from SQL Server is used in an R script and then returned to SQL Server.
SQL Server typeR classType in RESULT SETComments
smalldatetimePOSIXctdatetimeRepresented as GMT
smallmoneynumericfloat
datetimePOSIXctdatetimeRepresented as GMT
moneynumericfloat
uniqueidentifiercharactervarchar(max)
numeric(p,s)numericfloat
decimal(p,s)numericfloat
datePOSIXctdatetimeRepresented as GMT
tinyintintegerint
bitlogicalbit
smallintintegerint
intintegerint
floatnumericfloat
realnumericfloat
bigintnumericfloat
binary(n)

n <= 8000
rawvarbinary(max)Only allowed as input parameter and output
char(n)

n <= 8000
charactervarchar(max)
varbinary(n)

n <= 8000
rawvarbinary(max)Only allowed as input parameter and output
varchar(n)

n <= 8000
charactervarchar(max)
varbinary(max)rawvarbinary(max)Only allowed as input parameter and output
https://msdn.microsoft.com/en-us/library/mt590948.aspx

Tuesday, June 7, 2016

About-R_DataScienceR

R Data Science Tutorials


Important Questions


Common DataFrame Operations

Learning R

Caret Package in R

R Cheatsheets

Reference Slides

Using R for Multivariate Analysis

Time Series Analysis

Bayesian Inference

Machine Learning using R

Neural Networks in R

Sentiment Analysis

Imputation in R

NLP and Text Mining in R

Visualisation in R

Statistics with R

Useful R Packages

Market Basket Analysis in R


SPECIAL THANKS TO M. KARN https://github.com/ujjwalkarn/DataScienceR

Wednesday, June 1, 2016

About-R_PrincipalComponentsRegression

Principal Components Regression, Pt.1: The Standard Method

http://www.win-vector.com/blog/2016/05/pcr_part1_xonly/

Principal Components Regression, Pt. 2: Y-Aware Methods

http://www.win-vector.com/blog/2016/05/pcr_part2_yaware/

Principal Components Regression, Pt. 3: Picking the Number of Components

http://www.win-vector.com/blog/2016/05/pcr_part3_pickk/

Principal Components Regression in R, an operational tutorial

http://blog.revolutionanalytics.com/2016/05/principal-components-tutorial.html

Principal Components Regression in R: Part 2

http://blog.revolutionanalytics.com/2016/05/principal-components-regression-part-2.html

Principal Components Regression in R: Part 3

http://blog.revolutionanalytics.com/2016/05/principal-components-regression-in-r-part-3.html

Tuesday, May 31, 2016

About-BestPractices

Code readability is a universal subject in the world of computer programming. It's one of the first things we learn as developers. This article will detail the fifteen most important best practices when writing readable code.
Diomidis Spinellis, author of Code Quality: The Open Source Perspective, lists the most important rules for writing sparkling code. Follow them, and your code will look professional, live long, grow smoothly, and earn your colleagues’ love (rather than swearing).

Best Practice List

  1. Use Naming Conventions
  2. Create Descriptive Names: Use long descriptive names, like complementSpanLength, to help yourself, now and in the future, as well as your colleagues to understand what the code does. The only exception to this rule concerns the few key variables used within a method's body, such as a loop index, a parameter, an intermediate result, or a return value.
  3. Use Commenting Conventions
  4. Commenting & Documentation: IDE's (Integrated Development Environment) have come a long way in the past few years.
  5. Use Coding Conventions
  6. Consistent Indentation: I assume you already know that you should indent your code. However, it's also worth noting that it is a good idea to keep your indentation style consistent.
  7. Avoid Obvious Comments: Commenting your code is fantastic; however, it can be overdone or just be plain redundant.
  8. Code Grouping: More often than not, certain tasks require a few lines of code. It is a good idea to keep these tasks within separate blocks of code, with some spaces between them.
  9. Consistent Naming Scheme: PHP itself is sometimes guilty of not following consistent naming schemes. First of all, the names should have word boundaries like camelCase (First letter of each word is capitalized, except the first word) or underscores (Underscores between words, mysql_real_escape_string).
  10. Be Consistent: Do similar things in similar ways. If you're developing a routine whose functionality resembles that of an existing routine, use a similar name, the same parameter order, and a comparable structure for the code body.
  11. Don't Overdesign: Keep your design focused on today's needs. Your code can be general to accommodate future evolution, but only if that doesn't make it more complex. Don't create parameterized classes, factory methods, deep inheritance hierarchies, and arcane interfaces to solve problems that don't yet exist—you can't guess what tomorrow will bring.
  12. Split Your Code into Short, Focused Units: Every method, function, or logical code block should fit on a reasonably-sized screen window (25–50 lines long). If it's longer, split it into shorter pieces. An exception can be made for simple repetitive code sequences.
  13. Use Efficient Data Structures and Algorithms: Simple code is often more maintainable than equivalent code hand-tuned for efficiency. Fortunately, you can combine maintainability with efficiency by utilizing the data structures and algorithms provided by your programming framework. Use maps, sets, vectors, and the algorithms that work on them, and your code will be clearer, more scalable, faster, and memory-frugal.
  14. Use Framework APIs and Third-Party Libraries: Learn what functionality is available through an API in your programming framework, and also what's commonly available through mature, widely adopted third-party libraries. Libraries supported by your system's package manager are often a good bet. Use that code, resisting the temptation to reinvent the wheel.
  15. DRY Principle: DRY stands for Don't Repeat Yourself. Also known as DIE: Duplication is Evil.
  16. Avoid Deep Nesting: Too many levels of nesting can make code harder to read and follow.
  17. Limit Line Length: Our eyes are more comfortable when reading tall and narrow columns of text.
  18. Check for Errors and Respond to Them: Routines can return with an error indication, or they can raise an exception. Deal with it. Don't assume that a disk will never fill up, your configuration file will always be there, your application will run with the required permissions, memory-allocation requests will always succeed, or that a connection will never time out.
  19. Include Unit Tests: The complexity of modern software makes it expensive to deploy a system and difficult to test it as a black box. A more productive approach is to accompany every small part of your code with tests that verify its correct function. This approach simplifies debugging by allowing you to catch errors early, close to their source. Unit testing is indispensable when you program with dynamically typed languages such as Python and JavaScript, because they'll only catch at run-time any errors that that a statically typed language such as Java, C#, or C++ would catch at compile time. Unit testing also allows you to refactor the code with confidence.
  20. Keep Your Code Portable: Unless you have some compelling reason, avoid using functionality that's available only on a specific platform or framework. Don't assume that particular data types (such as integers, pointers, and time) are of a given width (for example, 32 bits), because this differs between various platforms. Store the program's messages separately from the code, and don't hard-code cultural conventions such as a decimal separator or date format.
  21. Make Your Code Buildable: A single command should build your code into a form that's ready for distribution. The command should allow you to perform fast incremental builds and run the required tests. To achieve this goal, use a build automation tool like Make, Apache Maven, or Ant. Ideally, you should set up a continuous integration system that will check, build, and test your code every time you make a change.
  22. Put Everything Under Version Control: All elements of your system—code, documentation, tool sources, build scripts, test data—should be under version control.
  23. Avoid Security Pitfalls: Modern code rarely works in isolation. Therefore it will inevitably risk becoming the target of malicious attacks. They don't have to come from the Internet; the attack vector could be data fed into your application. Depending on your programming language and application domain, you might have to worry about buffer overflows, cross-site scripting, SQL injection, and similar problems.
  24. File and Folder Organization: Technically, you could write an entire application code within a single file. But that would prove to be a nightmare to read and maintain.
  25. Consistent Temporary Names: Normally, the variables should be descriptive and contain one or more words. But, this doesn't necessarily apply to temporary variables. They can be as short as a single character.
  26. Capitalize SQL Special Words: Database interaction is a big part of most web applications. If you are writing raw SQL queries, it is a good idea to keep them readable as well.
  27. Separation of Code and Data: This is another principle that applies to almost all programming languages in all environments. In the case of web development, the "data" usually implies HTML output.
  28. Alternate Syntax Inside Templates: You may choose not to use a fancy template engine, and instead go with plain inline PHP in your template files.
  29. Object Oriented vs. Procedural: Object oriented programming can help you create well structured code. But that does not mean you need to abandon procedural programming completely. Actually creating a mix of both styles can be good.
  30. Read Open Source Code: Open Source projects are built with the input of many developers. These projects need to maintain a high level of code readability so that the team can work together as efficiently as possible.

Wednesday, May 25, 2016

About-VisualStudio2015-Extensions

VS2015 Update 2

It is better to have the latest visual studio with its companion extension for web development

VS2015 Extensions

Web Extension Pack The easiest way to set up Visual Studio for the ultimate web development experience.
Web Essentials Web Essentials is very well-named, it's essential, there is a whole host of features that Web Essential adds to Visual Studio, from Browser Link to its amazing addition to CSS intellisense and zen coding and more.
Bootstrap Snippet Pack The same thing as jQuery snippet, but this time it's for Bootstrap, it makes your life a lot easier and you don't have to look for examples on the web anymore, just type "<bs-Something" and you're good to go.
CssCop I'm not much of a CSS guru and I mostly rely on CssCop to point out my CSS mistakes, CssCop can check your Css based on set of rules and make sure you are following the best practices and avoid nasty mistakes.
jQuery Code Snippets This one is pretty self explanatory, it extends the Visual Studio jQuery snippets, and you don't need to spend your time typing $.ajax and what comes after it, it makes your jQuery coding speed a lot faster.
Node.js Tools for Visual Studio-NTVS NTVS is a free, open source plugin that turns Visual Studio into a Node.js IDE. It is designed, developed, and supported by Microsoft and the community.
ASP.NET MVC Boilerplate Are you sick and tired of adding must use packages and tools like elmah, glimpse, etc. to your project every time you want to start a project? Well, ASP.NET MVC Boilerplate addresses this issue, it is a template with all the packages and components that we use in every project, but it is not in default Visual Studio template.
SideWaffle Template Pack Another amazing extension, it adds tons of templates to the Visual Studio template, from templates for creating jQuery plugin to Angularjs Project template, it also makes creating custom templates a whole lot easier.
JSLint.NET JsLint is a code quality and validation tool for JavaScript, but this particular extension is a wrapper for Douglas Crockford's JSLint that bring this amazing tool to .NET.
JavaScript Map Parser Extension for Visual Studio that shows a map for JavaScript code in a separate panel.
Copy As HTML Copy selected code in HTML format while preserving syntax highlighting, indentation, background colour and font. Options: Max Height, Title Block, Alternating Lines, Line Numbers, Wrap Lines, Un-indent, Background Colour, Class Names. Converts RTF, outputted by VS, into HTML.
Image Optimizer Uses industry standard tools to optimize any JPEG, PNG and Gifs - including animated Gifs. Can do both lossy and lossless optimization.
Developer Assistant Searching for code sample couldn't be any easier with this extension, you can see and use sample code right from your code as you are typing.
Ref12 Ref12 is a pretty simple but necessary extension, it gives you the ability to navigate to the actual source code of the .NET Framework rather than just meta data.
Code Cracker for C# In the past, only big companies like JetBrain (Creator of Resharper) could build a code analysis feature and tooling for Visual Studio, because doing so was not an easy task and required a lot of work and money, but with release of Roslyn compiler and set of API that it provides for querying and working with compiler, it is easier to build a code analysis tool for C# and VB.
Refactoring Essentials Another code analyzer for C# and VB.NET is Refactoring Essential, it is available as both extension and Nuget package that you can use on project by project bases. https://www.nuget.org/packages/RefactoringEssentials/ Install-Package RefactoringEssentials
ResXManager Manage localization of all ResX-Based resources in one place. Shows all resources of a solution and let's you edit the strings and their localizations in a well-arranged data grid.
Visual Studio Spell Checker This extension spellchecks your comments, strings, and plain text, it's pretty non-intrusive and doesn't get in the way of your actual code and doesn't mix itself with error highlighting.
Productivity Power Tools A lot of native Visual Studio capability that we see today was originally available as an addition in this extension and eventually got integrated in Visual Studio, but this extension still has a lot of great features.
Snippet Designer Typing repetitive stuff pretty much bores me to death, I often create snippets for these kind of codes, snippet designer helps you do it easily and painlessly.
Chutzpah Test Adapter for the Test Explorer Chutzpah adapter for the Visual Studio Unit Test Explorer. Chutzpah is an open source JavaScript test runner which enables you to run JavaScript unit tests from the command line and from inside of Visual Studio.
Chutzpah Test Runner Context Menu Extension Chutzpah is an open source JavaScript test runner which helps you integrate JavaScript unit testing into your website. It enables you to run JavaScript unit tests from the command line and from inside of Visual Studio.
CodeMaid CodeMaid is an open source Visual Studio extension to cleanup and simplify our C#, C++, F#, VB, PHP, PowerShell, R, JSON, XAML, XML, ASP, HTML, CSS, LESS, SCSS, JavaScript and TypeScript coding.
VSColorOutput Color output for build and debug windows.

HTMLCode

HTMLCode Content