Saturday, March 31, 2018

R - Installing R Studio in Ubuntu 16.04

To install R Studio in ubuntu 16.04 follow the steps:

Step 1: Download the R Studio package:

$ wget --tries=3 --timeout=120 https://download1.rstudio.org/rstudio-xenial-1.1.383-amd64.deb
--2018-03-31 08:16:11--  https://download1.rstudio.org/rstudio-xenial-1.1.383-amd64.deb
Resolving download1.rstudio.org (download1.rstudio.org)... 54.230.190.45, 54.230.190.44, 54.230.190.119, ...
Connecting to download1.rstudio.org (download1.rstudio.org)|54.230.190.45|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 68194982 (65M) [application/x-deb]
Saving to: ‘rstudio-xenial-1.1.383-amd64.deb’

rstudio-xenial-1.1.383-amd64.deb     100%[===================================================================>]  65.04M   957KB/s    in 75s     

2018-03-31 08:17:26 (887 KB/s) - ‘rstudio-xenial-1.1.383-amd64.deb’ saved [68194982/68194982]


Step 2: Install R Studio:

$ sudo dpkg -i rstudio-xenial-1.1.383-amd64.deb
Selecting previously unselected package rstudio.
(Reading database ... 289186 files and directories currently installed.)
Preparing to unpack rstudio-xenial-1.1.383-amd64.deb ...
Unpacking rstudio (1.1.383) ...
dpkg: dependency problems prevent configuration of rstudio:
 rstudio depends on libjpeg62; however:
  Package libjpeg62 is not installed.

dpkg: error processing package rstudio (--install):
 dependency problems - leaving unconfigured
Processing triggers for shared-mime-info (1.5-2ubuntu0.1) ...
Processing triggers for hicolor-icon-theme (0.15-0ubuntu1) ...
Processing triggers for gnome-menus (3.13.3-6ubuntu3.1) ...
Processing triggers for desktop-file-utils (0.22-1ubuntu5.1) ...
Processing triggers for bamfdaemon (0.5.3~bzr0+16.04.20160824-0ubuntu1) ...
Rebuilding /usr/share/applications/bamf-2.index...
Processing triggers for mime-support (3.59ubuntu1) ...
Errors were encountered while processing:
 rstudio



Step 3: Since, errors were encountered, let us install the missing dependencies:

$ sudo apt-get install -f
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Correcting dependencies... Done
The following packages were automatically installed and are no longer required:
  linux-headers-4.13.0-32 linux-headers-4.13.0-32-generic linux-image-4.13.0-32-generic linux-image-extra-4.13.0-32-generic
  linux-signed-image-4.13.0-32-generic
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
  libjpeg62
The following NEW packages will be installed:
  libjpeg62
0 upgraded, 1 newly installed, 0 to remove and 126 not upgraded.
1 not fully installed or removed.
Need to get 78.9 kB of archives.
After this operation, 188 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://in.archive.ubuntu.com/ubuntu xenial/universe amd64 libjpeg62 amd64 1:6b2-2 [78.9 kB]
Fetched 78.9 kB in 1s (48.9 kB/s)    
Selecting previously unselected package libjpeg62:amd64.
(Reading database ... 291031 files and directories currently installed.)
Preparing to unpack .../libjpeg62_1%3a6b2-2_amd64.deb ...
Unpacking libjpeg62:amd64 (1:6b2-2) ...
Setting up libjpeg62:amd64 (1:6b2-2) ...
Setting up rstudio (1.1.383) ...
Processing triggers for libc-bin (2.23-0ubuntu10) ...


Installation is now complete. Let us verify the installation :

$ which rstudio
/usr/bin/rstudio


We can verify that the installation is now complete.


Monday, March 26, 2018

NodeJS - REPL - List all built in modules

To list all built in modules using nodejs REPL.

$ node
> var myRepl = require('repl');null;
null
> myRepl._builtinLibs;
[ 'assert',
  'async_hooks',
  'buffer',
  'child_process',
  'cluster',
  'crypto',
  'dgram',
  'dns',
  'domain',
  'events',
  'fs',
  'http',
  'https',
  'net',
  'os',
  'path',
  'perf_hooks',
  'punycode',
  'querystring',
  'readline',
  'repl',
  'stream',
  'string_decoder',
  'tls',
  'tty',
  'url',
  'util',
  'v8',
  'vm',
  'zlib',
  'http2' ]

> .exit
 $ 


There is another way to list the builtin modules without opening the Nodejs repl. Run the following command at your linux terminal:

$ node -pe "require('repl')._builtinLibs"
[ 'assert',
  'async_hooks',
  'buffer',
  'child_process',
  'cluster',
  'crypto',
  'dgram',
  'dns',
  'domain',
  'events',
  'fs',
  'http',
  'https',
  'net',
  'os',
  'path',
  'perf_hooks',
  'punycode',
  'querystring',
  'readline',
  'repl',
  'stream',
  'string_decoder',
  'tls',
  'tty',
  'url',
  'util',
  'v8',
  'vm',
  'zlib',
  'http2' ]
$

This I learnt from the answer posted at stackoverflow:


Friday, March 23, 2018

Julia - Language - Connect to Postgres DB - select statement

Below is the connection script, we can use to retrieve data from a PostGres Database using Julia Language:

$ julia
               _
   _       _ _(_)_     |  A fresh approach to technical computing
  (_)     | (_) (_)    |  Documentation: https://docs.julialang.org
   _ _   _| |_  __ _   |  Type "?help" for help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 0.6.2 (2017-12-13 18:08 UTC)
 _/ |\__'_|_|_|\__'_|  |  Official http://julialang.org/ release
|__/                   |  x86_64-pc-linux-gnu

julia> 

using NamedTuples;
using DataStreams;
using LibPQ;

v_postgres_host = "my.postgresServer.us-west-1.rds.amazonaws.com"; # Input the hostname here
v_postgres_dbname = "myPostGresDB"; # input the db name here 
v_postgres_user = "mahesh";   # input the postgres user
v_postgres_password = "myPass";  # input the password for the postgres user here 
v_port ="5432"; # input the port number of postgres db here
#create the connection here 
conn_string = "host=" *v_postgres_host *" port=" *v_port *" dbname=" *v_postgres_dbname *" user=" *v_postgres_user *" password=" *v_postgres_password *"";
conn = LibPQ.Connection(conn_string);
data = fetch!(NamedTuple, execute(conn, "SELECT department_id FROM school_table WHERE subject_name = \$1", ["physics"]));
close(conn);


To retrieve information from the data variable use the following:
data[:department_id]


Thursday, March 22, 2018

NodeJS - telnet - create echo server

Let us create a echo server using Node. For this, Open the terminal of Node REPL:

~ $ node

This will open the node prompt. Now, let us include the 'net' module.

> var net = require('net');
undefined

Implement the echo server using the following code:

> var server = net.createServer(function(socket){socket.on('data', function(data){socket.write(data);});});
undefined

Now, turning on the listener at a particular port number:
> server.listen(3000);
Server {
  domain: 
   Domain {
     domain: null,
     _events: { error: [Function: debugDomainError] },
     _eventsCount: 1,
     _maxListeners: undefined,
     members: [] },
  _events: { connection: [Function] },
  _eventsCount: 1,
  _maxListeners: undefined,
  _connections: 0,
  _handle: 
   TCP {
     reading: false,
     owner: [Circular],
     onread: null,
     onconnection: [Function: onconnection],
     writeQueueSize: 0 },
  _usingSlaves: false,
  _slaves: [],
  _unref: false,
  allowHalfOpen: false,
  pauseOnConnect: false,
  _connectionKey: '6::::3000',
  [Symbol(asyncId)]: 196 }


In a new terminal window, connect to the localhost server using telnet:

$ telnet 127.0.0.1 3000
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
hey 
hey 
hello
hello

We can see that, our messages are being echoed back. Hence, our implementation of a echo server is successful. Hence, we now exit the Node REPL prompt.

> .exit
~ $ 

Now, let us observe the response the telnet window:

$ telnet 127.0.0.1 3000
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
hey 
hey 
hello
hello
Connection closed by foreign host.
 $ 


We see that the telnet connection got closed as expected.

Wednesday, March 21, 2018

NodeJS - REPL - read-eval-print-loop

Node provides a REPL (read-eval-print-loop) interface, available by running node at the command line without any arguments. In a REPL we can write snippets of code - to get immediate results as each statement is written and executed.

REPL can be great for:

  • learning a programming language, 
  • running simple tests
  • debugging. 


Type node at the command prompt to start REPL:

~ $ node
> console.log("welcome to node")
welcome to node
undefined
> console.log(4*5)
20
undefined
> 4*5
20
> .help
.break    Sometimes you get stuck, this gets you out
.clear    Alias for .break
.editor   Enter editor mode
.exit     Exit the repl
.help     Print this help message
.load     Load JS from a file into the REPL session
.save     Save all evaluated commands in this REPL session to a file
> .exit
~ $ 


Reference:
NODE - repl and special keys 

Sunday, March 11, 2018

NodeJS - install package - plotly.js

Installing a package using npm:


First, let us search for the existance of the package - plotly.js

:~$ npm search plotly.js 
NAME                      | DESCRIPTION          | AUTHOR          | DATE       | VERSION  | KEYWORDS                                   
plotly.js                 | The open source…     | =alexcjohnson…  | 2018-03-09 | 1.35.2   | graphing plotting data visualization plotly
:~$ 


Now, we install plotly.js using the following command:

~$ npm install plotly.js
npm WARN deprecated nomnom@1.8.1: Package no longer supported. Contact support@npmjs.com for more info.
npm WARN saveError ENOENT: no such file or directory, open '/home/ubuntu/package.json'
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN enoent ENOENT: no such file or directory, open '/home/ubuntu/package.json'
npm WARN ubuntu No description
npm WARN ubuntu No repository field.
npm WARN ubuntu No README data
npm WARN ubuntu No license field.

+ plotly.js@1.35.2
added 495 packages in 70.718s
~$

After, plotly.js is installed we can check for its documentation using the explore command:

~$ npm explore plotly.js
~$

Looks like local documentation is not available. Hence, let us check its online documentation using the following command:

~$ sudo npm docs plotly.js

This command opens the online documentation in your default browser.




NodeJS - The node stack




NodeJS - npm - search for packages

In NodeJS , to search for packages , we can use the command line utility of npm. Say we want to search for the plotting libraries available with node.

$ npm search plot
NAME                      | DESCRIPTION          | AUTHOR          | DATE       | VERSION  | KEYWORDS                
plot                      | Play your plot…      | =mgg            | 2017-11-10 | 0.0.2    | plot scenario story line
plotly.js                 | The open source…     | =alexcjohnson…  | 2018-03-09 | 1.35.2   | graphing plotting data visualization plotly
@nteract/transform-plotly | Plotly Transform     | =cabhishek…     | 2018-03-07 | 3.1.6    | 
plotly                    | Simple node.js…      | =alexander-dan… | 2015-12-30 | 1.0.6    | plotting graphs interactive data visualization
@superdyzio/plotly.js     | The open source…     | =superdyzio     | 2018-02-06 | 1.33.1   | graphing plotting data visualization plotly
react-plotly.js           | A plotly.js react…   | =plotly         | 2018-03-07 | 1.7.0    | graphing plotting data visualization plotly react
plot-grid                 | Plot grid component  | =dfcreative     | 2017-06-12 | 2.4.2    | plot grid lines axis logarithmic decibels frequency 
colormap                  | Great looking color… | =bpostlethwaite | 2017-09-28 | 2.3.0    | colormap color map color hex rgb color-space cubehel
regl-scatter2d            | Scatter2d plot…      | =dfcreative     | 2018-02-20 | 2.1.17   | regl plotly
chartjs-node              | Create Chart.js…     | =sedouard       | 2018-01-12 | 1.6.1    | Chartjs Charts Plots Graphs
sparkly                   | Generate sparklines… | =sindresorhus   | 2015-12-17 | 3.1.2    | spark sparkly line sparkline sparklines unicode data
@xudafeng/plotjs          | plotjs               | =xudafeng       | 2018-01-02 | 1.0.1    | plot
gerber-plotter            | Transform stream…    | =mcous          | 2016-07-23 | 1.1.0    | PCB Gerber drill circuit board transform stream hard
regl-error2d              | Render error bars…   | =dfcreative     | 2018-01-05 | 2.0.4    | regl plotly
umlplot                   | umlplot              | =xudafeng       | 2018-01-11 | 1.0.1    | plot uml
asciichart                | Nice-looking…        | =x84 =xpl       | 2017-12-10 | 1.5.7    | asciichart ascii chart charting charts console termi
snap-points-2d            | snap round 2d points | =bpostlethwaite… | 2017-08-28 | 3.2.0    | snap round iterated plot 2d data vis
aframe-plot-component     | Renders 3D plots of… | =mikebolt       | 2017-08-30 | 1.1.2    | aframe aframe-component aframe-vr vr mozvr webvr plo
@nteract/plotly           | A minimal version…   | =hasch =jdetle… | 2017-04-11 | 1.0.0    | plotly dist
regl-line2d               | Draw polyline with…  | =dfcreative     | 2018-02-19 | 2.1.5    | regl line2d gl-vis gl webgl polyline plotly
~$ 

There is a long pause initially, if you are searching for the first time this is because npm downloads repository information. There is also an online version:

https://www.npmjs.com/search


NodeJS - server installation - Ubuntu

Step 1: Get node setup file and rename as nodesource_step.sh

~$ wget https://deb.nodesource.com/setup_8.x -O nodesource_setup.sh
--2018-03-11 09:22:15--  https://deb.nodesource.com/setup_8.x
Resolving deb.nodesource.com (deb.nodesource.com)... 52.84.110.132, 52.84.110.18, 52.84.110.122, ...
Connecting to deb.nodesource.com (deb.nodesource.com)|52.84.110.132|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 12546 (12K) [text/plain]
Saving to: ‘nodesource_setup.sh’

nodesource_setup.sh                  100%[===================================================================>]  12.25K  --.-KB/s    in 0s      

2018-03-11 09:22:15 (85.2 MB/s) - ‘nodesource_setup.sh’ saved [12546/12546]

Step 2: Install the PPA to your configuration using the below command:
~$ sudo bash nodesource_setup.sh
[sudo] password for ubuntu: 

## Installing the NodeSource Node.js v8.x LTS Carbon repo...


## Populating apt-get cache...

+ apt-get update
Ign:1 http://dl.google.com/linux/chrome/deb stable InRelease
Hit:2 http://security.ubuntu.com/ubuntu xenial-security InRelease                                           
Hit:3 http://in.archive.ubuntu.com/ubuntu xenial InRelease                                                  
Hit:4 http://dl.google.com/linux/chrome/deb stable Release              
Hit:5 http://in.archive.ubuntu.com/ubuntu xenial-updates InRelease      
Hit:6 http://in.archive.ubuntu.com/ubuntu xenial-backports InRelease
Hit:8 https://packagecloud.io/slacktechnologies/slack/debian jessie InRelease
Reading package lists... Done

## Confirming "xenial" is supported...

+ wget -qO /dev/null -o /dev/null 'https://deb.nodesource.com/node_8.x/dists/xenial/Release'

## Adding the NodeSource signing key to your keyring...

+ wget -qO- https://deb.nodesource.com/gpgkey/nodesource.gpg.key | apt-key add -
OK

## Creating apt sources list file for the NodeSource Node.js v8.x LTS Carbon repo...

+ echo 'deb https://deb.nodesource.com/node_8.x xenial main' > /etc/apt/sources.list.d/nodesource.list
+ echo 'deb-src https://deb.nodesource.com/node_8.x xenial main' >> /etc/apt/sources.list.d/nodesource.list

## Running `apt-get update` for you...

+ apt-get update
Ign:1 http://dl.google.com/linux/chrome/deb stable InRelease
Hit:2 http://dl.google.com/linux/chrome/deb stable Release                                                  
Get:3 https://deb.nodesource.com/node_8.x xenial InRelease [4,646 B]                                        
Hit:4 http://security.ubuntu.com/ubuntu xenial-security InRelease                                           
Hit:5 http://in.archive.ubuntu.com/ubuntu xenial InRelease               
Hit:7 http://in.archive.ubuntu.com/ubuntu xenial-updates InRelease
Get:8 https://deb.nodesource.com/node_8.x xenial/main Sources [761 B]
Get:9 https://deb.nodesource.com/node_8.x xenial/main amd64 Packages [1,003 B] 
Hit:10 http://in.archive.ubuntu.com/ubuntu xenial-backports InRelease               
Get:11 https://deb.nodesource.com/node_8.x xenial/main i386 Packages [1,005 B]
Hit:12 https://packagecloud.io/slacktechnologies/slack/debian jessie InRelease
Fetched 7,415 B in 3s (2,177 B/s)
Reading package lists... Done

## Run `apt-get install nodejs` (as root) to install Node.js v8.x LTS Carbon and npm

~$ 

Step 3: Install Node JS:

~$ sudo apt-get install nodejs
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  libllvm4.0 linux-headers-4.10.0-28 linux-headers-4.10.0-28-generic linux-headers-4.13.0-26 linux-headers-4.13.0-26-generic
  linux-image-4.10.0-28-generic linux-image-4.13.0-26-generic linux-image-extra-4.10.0-28-generic linux-image-extra-4.13.0-26-generic
  linux-signed-image-4.10.0-28-generic linux-signed-image-4.13.0-26-generic
Use 'sudo apt autoremove' to remove them.
The following NEW packages will be installed:
  nodejs
0 upgraded, 1 newly installed, 0 to remove and 95 not upgraded.
Need to get 12.7 MB of archives.
After this operation, 61.3 MB of additional disk space will be used.
Get:1 https://deb.nodesource.com/node_8.x xenial/main amd64 nodejs amd64 8.10.0-1nodesource1 [12.7 MB]
Fetched 12.7 MB in 13s (928 kB/s)                                                                                                               
Selecting previously unselected package nodejs.
(Reading database ... 316819 files and directories currently installed.)
Preparing to unpack .../nodejs_8.10.0-1nodesource1_amd64.deb ...
Unpacking nodejs (8.10.0-1nodesource1) ...
Processing triggers for man-db (2.7.5-1) ...
Setting up nodejs (8.10.0-1nodesource1) ...
~$ 

Step 4: Check the Node Versions installed :
~$ nodejs -v
v8.10.0
~$ npm -v
5.6.0

Step 5: Install Build Essential:

~$ sudo apt-get install build-essential
Reading package lists... Done
Building dependency tree       
Reading state information... Done
build-essential is already the newest version (12.1ubuntu2).
The following packages were automatically installed and are no longer required:
  libllvm4.0 linux-headers-4.10.0-28 linux-headers-4.10.0-28-generic linux-headers-4.13.0-26 linux-headers-4.13.0-26-generic
  linux-image-4.10.0-28-generic linux-image-4.13.0-26-generic linux-image-extra-4.10.0-28-generic linux-image-extra-4.13.0-26-generic
  linux-signed-image-4.10.0-28-generic linux-signed-image-4.13.0-26-generic
Use 'sudo apt autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 95 not upgraded.
~$ 


source: https://nodejs.org/en/download/package-manager/#debian-and-ubuntu-based-linux-distributions

source: https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-16-04