Pretty print of R Jupyter Notebook inside a function - r

When calling head(...) in a cell in jupyter notebook, we get a nice table with the data
However if I want to do the same in a function, nothing is shown, so I have to use print(head(...)) but the result is less pretty as you can see
Is there a way to get the same type of output as the first image but in a function ?

Jupyter display functions for IRkernel are provided by the IRdisplay package, which gets installed together with IRkernel (by default). Just use:
IRdisplay::display(CO2)
Or load the library into the namespace first:
library(IRdisplay)
data("CO2")
a <- function() {
display(head(CO2))
}
a()
Which works as intended:

Related

Call notebook using R in Databricks

I have a R notebook in Databricks, that I want to call inside an other R notebook.
I know that to call the notebook_a, one should do :
%run /path/notebook_a
But I want to do it inside an R script. For example :
if(condition){ %run /path/notebook_a }
Of course this code does not work.
Thanks a lot.
You should be able to use dbutils.notebooks.run for calling another notebook, but there is a difference between it and %run:
dbutils.notebooks.run executes another notebook as a separate job, so no definitions, etc. is pulled into the context of the current notebook - you can communicated data via temp views, etc.
%run execute another notebook and pulls all definitions and sides effects into the context of the current notebook.
I'm not sure if dbutils.notebooks.run will help in your case.
P.S. I personally would recommend to use %run with notebooks that only define functions, not doing any calculations, etc. - in this case, even if you have %run this doesn't cause any side effect on your current context.

Coding r in Jupyter Lab (irkernel) - Using stringr package: The str_view() function fails

Can anyone use the str_view() function in Jupyter Lab?
Whenever the str_view() function is used, the output returns only a partial white screen and the function returns no values. No other stringr functions have any noticeable issues. This str_view() function does perform as expected in R studio.
Tried a work-around using that nifty Jupyter feature, "Create New View for Outputs" in attempts to output that partial failed-display But then things get really screwy. That New View window grows continuously larger, becomes unmanageable, becomes unstable, then crashes Jupyter.
library(dplyr)
library(stringr)
med <- as.matrix(read.csv("med.csv"))
med
str_length(med)
str_match(med, "Pill")
str_view(med, "Pill")
To the best of my knowledge, this is a standard install and configuration of python, Jupyter, and the Irkernel.
Therefore it would be nice to know if others are having a similar issue when using the str_view() function with the Irkernel, or whether it's the setup on this particular PC.
Screen print is below.
I think it's to do with the JupyterLab theme. Some things still don't display too well in JupyterLab's Dark theme. I've tried reproducing your issue below on JupyterLab 1.1.3:
JupyterLab

Don't print the name of the directory to stdout when using here::set_here('path')

I'm using the R package 'here' to define my working directory using the following command at the start of my script:
here::set_here(path='/path/to/my_directory/', verbose = F)
Every time I run the script it prints this to the console:
here() starts at /path/to/my_directory
Is there a way to suppress this output? I tried using the invisible() function but that didn't work...
The message you’re seeing is printed when you’re attaching the ‹here› package. Simply don’t do that (it’s unnecessary anyway) to prevent it.
Otherwise, load it as follows:
suppressPackageStartupMessages(library(here))
… yeah, not exactly elegant.

How to use a platform specific package in my R function

my R package has a function (my_func) that uses function(bar) from another package (foo) which only available on Unix. So I write my code this way, following the suggested packages in Writing R extension:
my_func = function(){
....
if (requireNamespace("foo", quietly = TRUE)) {
foo::bar(..) # also tried bar(...)
} else {
# do something else without `bar`
}
...
}
However, I keep getting such warning message when I run R CMD check:
'loadNamespace' or 'requireNamespace' call not declared from: foo
Is there any way to use platform specific packages without gives such warnings? I tried to include foo in the Description file and the warning disappeared, but then the package cannot be installed on windows because foo is not available for the win.
I think you misread that comment, though it is easy to do because the logic is not clear and incontrovertible.
In that first paragraph that talked about calling packages using require(package) they said it was OK for a test environment or vignette but not from inside a finished function to use library(package) or require(package).
This is considered bad form, because the packages you call inside a function will usurp the order of access your user has set up in her work environment when loading packages.
The method you use above:
package::function()
is the approved of way to use a function within a function without altering the current environment.
But in order to use that function you must still have that package installed on your current machine or in your current working environment (such as a virtualenv or ipython or jupytr..and yes R will work in all those environments).
You cannot run a function that R cannot see from the working environment...so you must have it installed, but not necessarily loaded, to call it.

Restart R within Rstudio

I'm trying to call a simple python script from within R using system2(). I've read some information I found vague that said if 'too much' memory is used, it won't work.
If I load a large dataset and use some information in it to use as arguments to pass into system2(), it will only work if I manually click "Restart R" in call Rstudio.
What I want:
df <- read.csv('some_large_file.csv')
###extracting some info called 'args_vec'
for(arg in args_vec){
system2('python', args)
}
This won't work as is. The for loop is simply passed over.
What I need:
df <- read.csv('some_large_file.csv')
###extracting some info called 'args_vec'
###something that 'restarts' R
for(arg in args_vec){
system2('python', args)
}
This answer doesn't quite get what I want. Namely, it doesn't work for me within Rstudio and it calls "system" (which presents the same problem as "system2" in this case). In fact, when I put the answer referenced above in my Rprofile.site file, it just immediately closed rstudio:
I tried the suggestion as a normal function (rather than using "makeActiveBinding", and it didn't quite work.
##restart R in r session -- doesn't work
makeActiveBinding("refresh", function() { system("R --save"); q("no") }, .GlobalEnv)
##nor did this:
refresh <- function() { system("R --save"); q("no") }
I tried a number of variations of these two options above, but this is getting long for what feels like a simple question. There's a lot I don't yet understand about the startup process and "makeActiveBinding" is a bit mysterious. Can anyone point me in the right direction?
In Rstudio, you can restart the R session by:
command/ctrl + shift + F10
You can also use:
.rs.restartR()
RStudio has this undocumented rs.restartR() which is supposed to do just that: restarting R.
However, it does not unload the packages that were loaded, nor does it clean the environment, so that I have some doubts about if it restarts R at all.
If you use RStudio, use the menu item Session > Restart R or the associated keyboard shortcut Ctrl+Shift+F10 (Windows and Linux) or Command+Shift+F10 (Mac OS). Additional keyboard shortcuts make it easy to restart development where you left off, i.e. to say “re-run all the code up to HERE”:
In an R script, use Ctrl+Alt+B (Windows and Linux) or Command+Option+B (Mac OS)
In R markdown, use Ctrl+Alt+P (Windows and Linux) or Command+Option+P (Mac OS)
If you run R from the shell, use Ctrl+D or q() to quit, then restart R.
Have you tried embedding the function call within the apply function, rather than a for loop?
I've had some pieces of code that ran the system out of memory in a for loop run perfectly with apply. It might help?
For those not limited to a command that want something that actually resets the system (no prior state, no loaded packages, no variables, etc.) you can select Terminate R from the Session menu.
It is a bit awkward (asks you if you are sure). If anyone knows something like clear all or really clear classes in MATLAB let me know!

Resources