R/Shiny WebApp logs, running in shell (Ubuntu crontab) - r

I'm running, using crontab, an R/Shiny WebApp. The crontab expression looks like this:
#reboot R -e 'shiny::runApp(...)' | tee /var/log/shiny-server.log
I've also tried
#reboot R -e 'shiny::runApp(...)' &> /var/log/shiny-server.log
but does the same: It prints to the file the header when you initialize R from shell. But it doesn't print any text about the page, i.e. when it loads, when it uploads something, when it crashes...
Do you know how to pipe the output of the logs of the WebApp to the file?
Thank you

I have been searching in the web and I didn't find any solution.
Instead, I changed the philosophy. I generated myself some logs from inside the Shiny app with the help of the sink() function.
Thank you.

Related

Unix Run Command is not generating the log for R Script

I am running two different commands to run two different R Scripts and one of it is generating log and other is not. I am unable to figure out why .. any pointers would be appreciated
Working and generating log/error
Rscript main.R '2021=Jun_2021.xlsx&2021=May_2021.xlsx' 2> output/test_err.txt | tee output/test_log.txt
Executes Script but Not generating log/error
Rscript main.R '2021=TESTFILE.xlsm&Param1=INZ01&param2=Test' 2> output/test_err.txt | tee output/test_log.txt
as it turned out, i had a script to cleanup the output folder and was deleting these .txt files in the process.
Appreciate your inputs guys, thanks.

Way to get jupyter server root directory

Consider following:
$ cd /home/mydir
$ jupyter notebook --port=8888
In plain English, I am running jupyter server from /home/mydir directory.
Is there a simple way to get this directory from within a notebook regardless if it's a R notebook or a Python notebook or whatever? Maybe there is some magic command or variable?
NOTE: getwd() is not an answer as it returns directory of a current notebook but not the jupyter server root.
I have a similar problem and found your post, although I don't see a viable solution yet. Eventually I did found a solution, although it works only because I only care about Linux and I only care about Python. The solution is this magic line:
J_ROOT = os.readlink('/proc/%s/cwd' % os.environ['JPY_PARENT_PID'])
(I put it in a module in my PYTHONPATH so that I can easily use it in any Python notebook.) See if it is good for you.
Remember that your iPtyhon is just a Python module, so you can execute any valid Python code in a cell. So, if you started your notebook and haven't executed any directory changes in your code, you should be able to retrieve your cwd with the following in a cell:
import os
os.getcwd()
But furthermore, you can execute shell commands in cells, so you can retrieve other information in the cell. For example:
!which jupyter
should give you the path to your jupyter executable.
Which then leads you to running something like:
!jupyter --paths
which should give you something similar to:
​
config:
/Users/yourdir/.jupyter
/usr/local/etc/jupyter
/etc/jupyter
data:
/Users/yourdir/Library/Jupyter
/usr/local/share/jupyter
/usr/share/jupyter
runtime:
/Users/yourdir/Library/Jupyter/runtime
Frankly I'm surprised that all this time later there is still no built-in way to do this. I have used Isaac To's solution on Linux but recently had to make a jupyter notebook portable to Windows as well. Simply using os.getcwd() is fragile because a cell using it to set your JUPYTER_ROOT_DIRECTORY can potentially be called again, after you have changed your working directory.
Here is what I came up with:
try:
JUPYTER_ROOT_DIRECTORY
except NameError:
JUPYTER_ROOT_DIRECTORY = os.getcwd()
I put it in one of the first couple cells with the initial import statements. If the cell gets called again, it will not re-set the variable value because the variable exists and does not throw an exception.
It should be noted that unlike Isaac To's solution it sets the value to the directory the current .ipynb was run from, which is not necessarily the same directory as the top-level dir the user can access in the left hand file pane.
My suggestion is to use an intuitive approach.
Create a new folder within the Jupyter environment with a very unique name, for example, T246813579.
You can now locate the Jupyter working path by searching in your file explorer. For example, you can use the Windows Explorer in order to locate your new folder.
The expected result should look something like this:
C:\Users\my_user_name\JupyterHome\T246813579
The answer from #Isaac works well for Linux, but not all systems have /proc. For a solution that works on macOS and Linux, we can use shell commands, taking advantage of the ! shell assignment syntax in Jupyter:
import os
JPY_ROOT = ! lsof -a -p {os.environ['JPY_PARENT_PID']} -d cwd -F n | tail -1 | cut -c 2-
JPY_ROOT = JPY_ROOT[0]
print(JPY_ROOT) # prints Jupyter's dir
Explanation:
Get the process ID (pid) of the current jupyter instance with os.environ['JPY_PARENT_PID']
Call lsof to list the process's open files, keeping only the current working dir (cwd)
Parse the output of lsof using tail and cut to keep just the directory name we want
The ! command returns a list, here having only one element
Alternate Version
To save the os import, we could also use shell commands to get the PID. We could also do the subsequent string wrangling in python, rather than calling tail and cut from the shell, as:
JPY_ROOT = ! lsof -a -p $(printenv | grep JPY_PARENT_PID | cut -d '=' -f 2) -d cwd -F n
JPY_ROOT = JPY_ROOT[2][1:]

Run shiny application with arguments in terminal

Like in the topic, I'd like to run shiny app with parameters. I need to specify the database file's path from which I will grab the data. The problem is that the file changes sometimes thus I have to modify the file.path every time.
This is the command I use when running application from terminal
R -e "shiny::runApp('../Shiny_visualization')"
I tried
R -e "shiny::runApp('../Shiny_visualization')" --args 'db_path' yet I got an error.

Shiny script from Ubuntu bash

I wanted to run with crontab (the system daemon used to execute desired tasks at certain times) a shiny script.
I first tried running sh Autorun.sh, being in the file:
R
shiny::runApp(...)
but that didn't work
I also tried writting directly Rscript shiny::runApp(...) but it also doesn't work
Any ideas?
To run code using R on shell, you must use the -e option, which stands for expression. The same thing can be done via Rscript.
The correct syntax is then:
R -e 'shiny::runApp(...)'
Care must be taken with the quotes if there are any in the expression being used.
For more information on other command-line options, check An Introduction to R - Appendix B.

Redirecting R help message to a text file

I have R installed on Linux. To get instant help I type on the prompt as:
?read.table
Is it possible to redirect this help message to a text file. For example, if I want Linux man page to be redirected to a text file, I write on the shell as:
$ man cp | col -b > cp_help.txt
Such text file is of great help as I can search within it, or scroll fast up and down or add my comments/examples for future or distribute to students.
R offers the tool Rscript to run R code from the console. You can pass a script as argument
Rscript myScript.R
but you can also directly enter an R command using the option -e. Now it is easy to redirect the help into a file
Rscript -e "?read.table" | col -b > read.table.txt

Resources