Friday, March 11, 2016

R library path

Resultado de imagen para turn it off and on again meme



I'll begin this post with something about me...

I'm a big fan of defaults. I love stuff that's ready to work "out of the box".

Well, having said that... my today's post is about preventing some weird stuff that happened yesterday to me for using the default R library as a shared library.
  • First things first. Enviroment
    I was using R 3.1.1 from Rstudio on Windows 8 ( yeah, I know... kindo nasty)
  • Second. What I did wrong
    I have to say in my defense, that I still don't think this is wrong, I guess It has more to do with some bad luck with the way I'd setup my consoles shortcuts. Well, to the point I had a script witch connects to a DB using RODBC and It takes some time. You know that RStudio is single threaded and I wanted to test it while continue working on something else. So I decided I would run it from a windows console ( I should've used an R terminal. But bear with me for arguments sake ).
And so It began..
Resultado de imagen para shouldn't have done that meme

  • It couldn't load any of the script libraries, eventhough they were physically in the default library
  • I tried to "install.package" to test one, and the process couldn't write in the directory... well I thought, "I guess I can't have Rstudio and this console accessing to the library". So I shutted down the console and returned to Rstudio.
  • Opened the script and... surprise. It couldn't find the library... WTF
  • Tried the infamous IT Crowd Algorithm "have you tried to turning it off and on again".. "Some temporal files have to be locked", I thought.. It's windows after all...
  • Tried again and still nothing happened.... well time to fix this stuff
The default library was unusable. The console I had used was configured to open with Admin Rights.....and I still don't know why It had locked all the stuff in the library dir...

  1. Change permitions didn't work
  2. Install packages within R as admin user didn't work either
  3. So I listed and configured another directory as user library
    R version 3.2.3 (2015-12-10) -- "Wooden Christmas-Tree"
    Copyright (C) 2015 The R Foundation for Statistical Computing
    Platform: x86_64-w64-mingw32/x64 (64-bit)
    
    R is free software and comes with ABSOLUTELY NO WARRANTY.
    You are welcome to redistribute it under certain conditions.
    Type 'license()' or 'licence()' for distribution details.
    
      Natural language support but running in an English locale
    
    R is a collaborative project with many contributors.
    Type 'contributors()' for more information and
    'citation()' on how to cite R or R packages in publications.
    
    Type 'demo()' for some demos, 'help()' for on-line help, or
    'help.start()' for an HTML browser interface to help.
    Type 'q()' to quit R.
    
    During startup - Warning message:
    Setting LC_CTYPE=en_US.UTF-8 failed
    > .libPaths()
    .libPaths()
    [1] "C:/Program Files/R/R-3.2.3/library"
    
    >.libPaths("xxxxx")
    
    .libPaths()
    [1] "C:/Users/xxx/R/win-library/3.2" "C:/Program Files/R/R-3.2.3/library"   
  4. And so it began working..... I installed a new R version after I learned I had to install most of my libs again

What did I learned


  • First: I don't have any shortcut with admin rights now.
  • Second: After some internet reading I learned that having a user library is a good practice
  • Third: I understand windows less every time
  • Fourth: The only useful thing I wrote was
    .libPaths()
Anyways, I hope this is useful to someone....I'll quit the rambling now....

Monday, March 07, 2016

Install Shinny & Rstudio over NginX

Installing Shinny & Rstudio Open Source over Nginx


Today I will install in Centos 7:
  • Shinny and Rstudio web
  • NginX
  • And will Configure Nginx to serve as proxy for Shinny and Rstudio Web
  • All of this using the most basic setup in Digital Ocean
    
    
    #create user
    useradd admin/crodriguez
    passwd xxxxxx
    
    vi /etc/sudoers
    
    
    #Add user to wheel group for admin priviledges
    gpasswd -a crodriguez wheel
    
    
    #Or make him kindoff rootie User privilege speficication ;)
    admin ALL=(ALL) ALL
    
    
    # From now remember to use the created user to run all commands
    
    # 0. If you have less than 8 GB  RAM
    #lets make swap bigger in 1 GB
    
    sudo /bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=1024
    sudo /sbin/mkswap /var/swap.1
    sudo /sbin/swapon /var/swap.1
    sudo sh -c 'echo "/var/swap.1 swap swap defaults 0 0 " >> /etc/fstab'
    
    #Now we're over with the preparations. Lets begin with the installation
    
    # 1. Lets install NgNx
    
    #install EPEL release
    sudo yum install epel-release
    sudo yum install nginx 
    
    #start the web server
    #in centos 7
     sudo systemctl start nginx
    
    # if you're using any flavor of ubuntu
    # sudo /etc/init.d/nginx start
    
    # Leave it as a service once the server starts
    sudo systemctl enable nginx
    
    # review if directory was created /usr/share/nginx/html
    
    # 2. install R from EPEL 
    sudo yum install R
    
    #3.  Download and install Rstudio for RH/Centos
    #change the version number if there's a newer release :) 
    
    wget http://download2.rstudio.org/rstudio-server-rhel-0.99.491-x86_64.rpm
    sudo yum install --nogpgcheck rstudio-server-rhel-0.99.491-x86_64.rpm
     
    
    #4. install "shinny" package from cran
    sudo su - -c "R -e \"install.packages('shiny', repos='http://cran.rstudio.com/')\""
    
    
    #5. Download shiny RPM
    
    wget https://download3.rstudio.org/centos5.9/x86_64/shiny-server-1.4.1.759-rh5-x86_64.rpm
    
    #6. Install shiny RPM :)
    
    sudo yum install --nogpgcheck shiny-server-1.4.1.759-rh5-x86_64.rpm
    
    
    #7. review shiny server status
    sudo systemctl status shiny-server
    
    #8 Create a group for shinny server config and server running
    
    sudo groupadd shiny-apps
    sudo usermod -aG shiny-apps crodriguez
    sudo usermod -aG shiny-apps shiny
    
    #change server directory owner and permissions
    #replace crodriguez for your user
    
    cd /srv/
    sudo  chown --recursive  crodriguez:shiny-apps shiny-server/
    #Change mode to all stuff in the dir
    cd shiny-server/
    sudo chmod g+w .
    sudo chmod g+s .
    
    #we're really changing permissions over some hard links. ( ls -l ) 
    
    lrwxrwxrwx 1 crodriguez shiny-apps 38 Jan 28 15:55 index.html -> /opt/shiny-server/samples/welcome.html
    lrwxrwxrwx 1 crodriguez shiny-apps 37 Jan 28 15:55 sample-apps -> /opt/shiny-server/samples/sample-apps
    
    #TODO everythin in  /srv/shiny-server will be served as an app
    
    #9. Config NGINX so we can use it as proxy for Rstudio and NGINX. For pretty URLs you know ;)
    
    sudo mkdir /etc/nginx/sites-available
    sudo mkdir /etc/nginx/sites-enabled
    
    
    #9.1 Include links to recently created config dirs in nginx.conf.
    # inside http{} block and before server{} block include the two lines *.conf and add some memory for server names domain 
    
    sudo vi /etc/nginx/nginx.conf
    
    include /etc/nginx/sites-enabled/*.conf;
    server_names_hash_bucket_size 64;
    
    
    
    #9.2 Add virtual locations on http{} block
    
            location /shiny/ {
               proxy_pass http://127.0.0.1:3838/;
               proxy_http_version 1.1;
               proxy_set_header Upgrade $http_upgrade;
               proxy_set_header Connection "upgrade";
            }
    
            location /rstudio/{
               proxy_pass http://127.0.0.1:8787/;
               proxy_http_version 1.1;
               proxy_set_header Upgrade $http_upgrade;
               proxy_set_header Connection "upgrade";
            }
    
    #10. restart Nginx
     sudo systemctl restart  nginx
    
    
    #10.1 Install Rmarkdown
    # Two easy steps
    
    #rebuild yum cache ( just because we haven't done this yet )
    sudo yum makecache fast
    
    #install libcurl / libcurl-devel y openssl openssl-devel
    sudo yum -y install libcurl libcurl-devel openssl openssl-devel
    
    #install R devtools
     sudo su - -c "R -e \"install.packages('devtools', repos='http://cran.rstudio.com/')\""
    
    #install rmarkdown
     sudo su - -c "R -e \"install.packages('rmarkdown', repos='http://cran.rstudio.com/')\""
    
    
    #11. If everything it's ok
    
    #verify installation
    whereis rstudio-server
    sudo rstudio-server verify-installation
    sudo rstudio-server status
    
    
    #Bonus. with devtools and git you can install packages from github but "git" its allways a nice thing to have  :)
    sudo yum install git
    
    
    
    
    This is how it should look

    1. NGINX

    2. Rstudio

    3. Shinny

    Wednesday, February 10, 2016

    Why do I use R ?

    Last two years I've been learning R and using it in my everyday work. And As much as It has been a change of mindset. I'm really happy with it. Much of my work about data clean up has been automated. And all the packages created by the community make my life easier.

    Here is a little list of the things I like the most

    1. Unlike SPSS or SAS it's open source
    2. It's a full programing language
    3. Has a nice graphical toolset
    4. It's platform independant (Yeap I just use windows at work, but who cares)
    5. It has a lot of analysis and data manipulation tools
    6. Did I mention is open source ?
    7. A helpfull community
    8. It's been growing in popularity the last 5 years not only in the studies field but as a real world work tool
    9. And most of all...It's free as in speech, so everybody can recreate anything. And help solving stuff

    I tested a bunch of IDEs and I love this one Rstudio. This one is free as in "free beer"
    It has all the features I need and more.