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