Run R Dashboard from command line on Windows 10

From the sales dashboard project I developed using R and shinydashboard, please read it if you haven’t. I created a small command line batch script to run it at the backend. In that project, I use Windows 10 with R package installed in the PC.

It is very simple script to run your finished R script from cmd batch command line. Use your R Studio, create a new text file, then save it as batch file, for example startserverdashboard.bat. Type the following line into your batch file:

"your-R-path-installation-bin\Rscript.exe" \your-R-script-folder\salesdashboard.R

Change your-R-script-folder to your actual folder where you saved your script.

To run the server script, open a terminal/cmd, got to the folder where you stored the script, then just type the batch file from command line:

startserverdashboard.bat

That’s all you have to do. The batch will run the script including web server with port to run the dashboard report. To stop the web server, click Ctrl-C, or just close the command line window.

Access the Dashboard from Mobile Phone

The above approach is simple, and already good enough for the needs. The owner can get sales numbers in chart dashboard as he/she wish. It is secured enough, since the dashboard is only accessible from the POS PC, where only the cashier and the owner can see it.

However, if you want to have easy access to the dashboard, for example by owner’s mobile phone as long as connected in the same network/wifi. This approach can be achieved by opening the IP and the port on the firewall setting. You can search from internet how to do it. You can still secure the dashboard by limiting who can access to the PC on the security setting.

Depends on your network settings, by default, the router on the network will give dynamic IP and the port. You need to change it so you will have static IP and port address to access the report. To do this, R web server need to be set to a specific static IP address and Port. Create new R script from your R Studio, type the following script:

require(shiny)
folder_address = '\\path-to-your-code-folder\\salesdashboard.R'

x <- system("ipconfig", intern=TRUE)
z <- x[grep("IPv4", x)]
ip <- gsub(".*? ([[:digit:]])", "\\1", z)
print(paste0("the Shiny Web application runs on: http://", ip, ":3838/"))

runApp(folder_address, launch.browser=FALSE, port = 3838, host = ip)

Change the path-to-your-code-folder to your R script folder. The port 3838 is default port setting that is used by shiny web server. You can change it as you wish. Don’t forget to allow the port number access in your firewall setting.

Now the owner can access the dashboard report, anywhere using mobile phone as long as he/she is in the cafe, in the same network/wifi.

Access Dashboard anywhere

If there are many shops in different locations you will need cloud solution. The dashboard can be access by the owner from home or from anywhere in the world. The R server is stored in the cloud. There are many R cloud based services available in AWS, Google, Microsoft Azzure, etc.

Source data is generated from each store, and then upload to the database in the cloud. R server then process all the data into dashboard reports. The cost is vary depends on what solution do you use. But nowadays the cost is scalable and adjust automatically accordingly as the growth of your business, means the bigger your business, the more data you will have, means more server resources you will need, at the end will add the cost you have to spend.

Develop charts for a small Coffee Shop

R is one of great tools available that one of features that I use is reporting, specifically charts. There are many nice professional looking chart libraries you can use. And it is pretty easy to develop in R. After developing an R script, next thing you will do is deploy it so your users can use it. There are many ways to deploy R script.

In this article I will show you how easy it is to deploy R script in Windows 10. The case is one of my client running a small coffee shop. All transactions are done using a POS (Point of Sales) software running in Windows 10 PC. The POS is an open source Java based software using Apache Derby database as the backend.

The owner asked me to help him develop a dashboard summary reports, in cue cards and charts presentation. Based on this requirement I propose a simple approach:

  1. Extract transaction data on daily basis to csv file.
  2. Develop dashboard using R and displayed in browser.
  3. User can see report from the PC.
  4. All processes are done in the same PC.

All works and development are done using R only running on the same PC used for POS. No additional hardware needed. The script is run automatically using Windows CMD. User able to see updated summary reports in the dashboard.

R Libraries that I use on this project:

  1. shiny
  2. shinydashboard
  3. ggplot2
  4. dplyr
  5. lubridate
  6. tidyverse

I completed the project in less than a week including setup, testing, training and deploy. My client is very happy with the result. I will share on the next post how I develop and deploy it.

Setup Mopidy on Raspberry PI

Mopidy plays music from local disk, Spotify, SoundCloud, TuneIn, and more. You can edit the playlist from any phone, tablet, or computer using a variety of MPD and web clients. It is written in Python. I use Mowecl music player extension for music web user interface.

I connect the raspberrypi to an amplifier with 4 speakers at my house. This setup is perfect for cafe, lounge, restaurant or lobby hotel. It has a very nice – easy to use – web user interface to play music, so you can control it from your PC/notebook or mobile phone connected to the raspberrypi via wifi or LAN. It can play music 24 hours a day with selected playlist.

In this post I’d like to share with you how to setup Mopidy for your music server using RaspberryPI. I use an old Raspberrypi model 2B. I use 16GB SDCARD attached to it, big enought to hold the OS and hundreds of music files.

I don’t discuss Raspberry PI setup on this post. If you haven’t setup your Raspberry PI, please check my previous post.

Setup Mopidy

First, you need to setup Mopidy.

Update Raspberrypi

sudo apt-get update
sudo apt-get upgrade

Add the archive’s GPG key

wget -q -O - https://apt.mopidy.com/mopidy.gpg | sudo apt-key add -

Add the APT repo to your package sources

sudo wget -q -O /etc/apt/sources.list.d/mopidy.list https://apt.mopidy.com/buster.list

Run Mopidy Install command

sudo apt-get update
sudo apt-get install mopidy

Configure Mopidy

sudo nano .config/mopidy/mopidy.con

Install some required libraries

First, you need to check python version.

Python3 --version

Then, if everything is ok, install Gstreamer >= 1.14.0

    sudo apt install \
    python3-gst-1.0 \
    gir1.2-gstreamer-1.0 \
    gir1.2-gst-plugins-base-1.0 \
    gstreamer1.0-plugins-good \
    gstreamer1.0-plugins-ugly \
    gstreamer1.0-tools

Install latest Release of Mopidy

sudo python3 -m pip install --upgrade mopidy

Check available Mopidy Extension

python3 -m pip search mopidy

Install Mopidy-Mowecl extension

python3 -m pip install Mopidy-Mowecl

Load Mopidy at Boot

sudo dpkg-reconfigure mopidy

Audio Settings

Set alsamixer volume. Ensure that audio output is set to 3.5mm jack.

sudo raspi-config

Select Advance – Audio – select 3.5mm jack

sudo alsamixer

Set volume level to 85, or adjust according to your need. Then save/store value.

sudo alsactl stores

Add user mopidy to video group

groups mopidy
sudo adduser mopidy video

After the installation is completed, open from your browser from your PC/notebook, you will see the nice, professional look of Mowecl with all music files in it:

Setup SSH key Windows 10 to Raspberry PI/Ubuntu

Create the RSA Key Pair

The first step is to create a key pair on the client machine (usually your computer):

ssh-keygen

You will be asked to save the key, press enter.

Generating public/private rsa key pair.
Enter file in which to save the key (C:\Users\CK/.ssh/id_rsa):
C:\Users\user /.ssh/id_rsa already exists.
Overwrite (y/n)? 

You will see the following result that the key is generated.

Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in C:\Users\user/.ssh/id_rsa.
Your public key has been saved in C:\Users\user/.ssh/id_rsa.pub.
The key fingerprint is:SHA256:LfX15TzDjZ0/KW/OPhufPRM8ZBAy9QkAcgOmIAFAjYz8c ck@DESKTOP-VT95ELK
The key's randomart image is:
+---[RSA 2048]----+
|X+.  . ..o+.o..  |
|+ . . .  . o ... |
| o = .    o   .o |
|. . + E  o . B...|
|     .  S . * B..|
|         .   oo++|
|               X+|
|              o+B|
|              .=B|
+----[SHA256]-----+

Copy your key to the server.

type .ssh\id_rsa.pub | ssh username@servername 'cat >> .ssh/authorized_keys'

It is done. You now can connect to your server without password.

Simple way to execute a command on startup in Ubuntu

I would like to share with you a simple way to execute commands on startup in Ubuntu. This tip is very useful for example I have an issue on the Netatalk service that do not run properly so every time I have to run the command to restart the service after I login to Ubuntu.

All commands that you need to be executed must be stored in etc/rc.local setting. Open terminal then execute this command:

sudo nano /etc/rc.local

Add the following command to restart Netatalk service, right before “exit 0":

sudo service netatalk restart
exit 0

That’s it! It’s so simple. I have solved the Netatalk service problem for now. So I have more time to take a look what is the problem with the Netatalk service.