[R-bloggers] Curried Functions (and 2 more aRticles)

[R-bloggers] Curried Functions (and 2 more aRticles)

Link to R-bloggers

Curried Functions

Posted: 16 Oct 2020 11:42 PM PDT

[This article was first published on TeachR, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

No, we are not talking about dinner,
although this post is giving me ideas about
what I should have for lunch.

To read about what function currying actually means,
please see the Wikipedia page.
(And while you are there, make a donation
already!)

Basically, you take a function of multiple arguments
and reduce it to a function with one argument.
I probably use the term incorrectly, because I also
consider currying to cover any reduction in the number
of function arguments, not just down to one.

Here's an example. I'm using the curry function
from my personal R package.

library(alrtools)  my_rnorm <- curry(rnorm, mean = 100, sd = 200)  print(my_rnorm)  ## function (n, mean = 100, sd = 200)   ## .Call(C_rnorm, n, mean, sd)  ##   

Now, every time I use my_rnorm the parameters
mean and sd are pre-set.

set.seed(95)  x <- my_rnorm(100e3)  mean(x)  ## [1] 99.52828  sd(x)  ## [1] 199.7653  

NOTE: my curry function still get a weird error with some functions
that are very general in the arguments they accept, like max and sum.
I'm still figuring that out.

There are other packages in R that support currying.
Namely functional.

library(functional)  f_rnorm <- Curry(rnorm, mu = -20, sd = 35)  print(f_rnorm)  ## function (...)   ## do.call(FUN, c(.orig, list(...)))  ##   

Peter Danenberg, the author of the functional package,
credits Byron Ellis
for the functional::Curry function.
I have an older version of alrtools:::curry_v1
that I copied from the same source. Fun times.

So, if this has existed since 2007,
why am I blogging about it?
Because I finally solved something annoying about Curry
and I just can't wait to share (with the two people who will read this).

Check out the function signatures for the two curried functions and the original:

args(f_rnorm)  ## function (...)   args(my_rnorm)  ## function (n, mean = 100, sd = 200)   args(rnorm)  ## function (n, mean = 0, sd = 1)   

f_rnorm doesn't tell you anything about the arguments that are left,
or the defaults that were set in the currying process.
Most of the time, no one cares because you just created the curried function
so why should you have forgotten the details about it?
Yes, true, but I want to use alrtools::curry to provide code for other
people to use, and they might like the visual cues.

Here are some cool things about my new version of curry

  1. The function signature shows all arguments
  2. Currying changes the function signature and simply includes defaults for the curried arguments
  3. You can add new arguments that before were not in the signature!
  4. You can override the curried arguments if you want (because the currying is done by specifying default values instead of storing the default values in the closure)
  5. The new function stays in the namespace of the original function, but is still listed in the global environment
  6. But, remember the caveat I gave above – I am still working through all the issues with dots
  7. I haven't tested this yet, but I am pretty sure that curried arguments will be able to reference other arguments, like in the function signature of dgamma

Just a note for those of you who like this topic but would rather use
a more reputable package, like functional. You can still get at
all the information you want within the function closure.
R let's you examine those, and I will show you how.
(Also, my package is licensed as CC0, so you can steal the code
and use it wherever you want, too. I don't care. Attribution is nice.)

max100 <- functional::Curry(max, 100)  max100(c(40, 20, -21))  ## [1] 100  max100(150, 34, -132)  ## [1] 150  ls(environment(max100), all.names = TRUE)  ## [1] "..."   ".orig" "FUN"  environment(max100)$.orig  ## [[1]]  ## [1] 100  environment(max100)$FUN  ## function (..., na.rm = FALSE)  .Primitive("max")  

Thanks for reading!

To leave a comment for the author, please follow the link and comment on their blog: TeachR.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The post Curried Functions first appeared on R-bloggers.

This posting includes an audio/video/photo media file: Download Now

Little useless-useful R function

Posted: 16 Oct 2020 04:20 PM PDT

[This article was first published on R – TomazTsql, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Nothing spectacular, but yet interesting little useless R function for playing with strings and chars.

Converting Sentence case text to mixture of all and small caps resulting in sentence in mixed case.

For example:

"This is useless R Function that seems to exists."

to:

 "This is UsEleSS r FuNcTion THaT SeEms To ExIsTS."

And many other mixture of cases. So by calling this function, the results will give you the mixed cases string.

  MixedCases <- function(stavek){  nov_stavek = ""  cifra = 0  crka = ""  is.upper <- "[A-Z]"  is.lower <- "[a-z]"      for (crka in strsplit(stavek, "")[[1]]){      if (nchar(nov_stavek)<2){        random_stevilka = sample(0:1, 1, replace=TRUE)        if (random_stevilka == 0){          nov_stavek = paste(nov_stavek,toupper(crka), sep = "")        }        else {          nov_stavek = paste(nov_stavek,tolower(crka), sep = "")        }      }     else{       if(( grepl(pattern = is.upper,  x=(strsplit(nov_stavek,"")[[1]][(cifra-2)])) &             grepl(pattern = is.upper,  x=(strsplit(nov_stavek,"")[[1]][(cifra-1)])) |                        grepl(pattern = is.lower,  x=(strsplit(nov_stavek,"")[[1]][(cifra-2)])) &             grepl(pattern = is.lower,  x=(strsplit(nov_stavek,"")[[1]][(cifra-1)]))   ) == TRUE){            if (  grepl(pattern = is.upper,  x=(strsplit(nov_stavek,"")[[1]][(cifra-1)])) ) {                nov_stavek = paste(nov_stavek, tolower(crka), sep = "")            }            else {             nov_stavek = paste(nov_stavek, toupper(crka), sep = "")                          }       }      else  {            random_stevilka = sample(0:1, 1, replace=TRUE)           if (random_stevilka == 0){            nov_stavek = paste(nov_stavek, toupper(crka), sep = "")             }          else {             nov_stavek = paste(nov_stavek, tolower(crka), sep = "")             }        }     }     #cifra = cifra + 1     }    return(nov_stavek)  }    

Talk about useless functions ๐Ÿ™‚

Code is also available at Github. Feel free to improve it with even more wackiness ๐Ÿ™‚

Happy Rrrring ๐Ÿ™‚

To leave a comment for the author, please follow the link and comment on their blog: R – TomazTsql.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The post Little useless-useful R function first appeared on R-bloggers.

rstudio::global(2021)

Posted: 15 Oct 2020 11:00 AM PDT

[This article was first published on RStudio Blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

a logo for the virtual event called rstudio::global(2021)

Back in July we made the difficult decision to cancel rstudio:conf(2021) for the health and safety of our attendees and the broader community. 
 

Today, we are thrilled to announce that rstudio::global(2021), our first ever virtual event focused on all things R and RStudio, now has a date locked in:  January 21, 2021!

Our goal is to make rstudio::global(2021) our most inclusive and global event, making the most of the freedom from geographical and economic constraints that comes with an online event.
That means that the conference will be free, designed around participation from every time zone, and have speakers from around the world.

What is rstudio::global(2021)?

  • 24-hour hour global virtual event (so make sure you don't run out of coffee!)

  • 3 awesome keynotes.
    More on that below!

  • 30 talks with live Q&A. Learn from a diverse array of experts about a wide range topics of interest to data scientists.

  • 20 rapid fire lightning talks.

  • 30+ birds of a feather (BoF) sessions.
    One of the best parts of rstudio::conf is connecting with so many amazing people in the data science community.
    The BoFs provide an opportunity to connect with people who are working on the same problems, in the same fields, and using the same tools.

  • Social events. Fun and interesting opportunities to connect with your peers. 

  • Diversity scholar program.
    We believe in building on-ramps so that people from diverse backgrounds can learn R, build their knowledge, and then contribute back to the community.
    Since this conference will be virtual, we will be opening up the program to participants from all over the world.
    Expect to hear more on this soon!

Who are the keynote speakers?

Vicki Boykis

Vicki Boykis is a machine learning engineer at Automattic, the company behind WordPress.com. She works mostly in Python, R, Spark, and SQL, and really enjoys building end-to-end data products. Outside of work she publishes the Normcore Tech newsletter and blogs at https://veekaybee.github.io/. In her “spare time”, she blogs, reads, and writes terrible joke tweets about data.

Vicky will discuss how that as people who can write code and analyze data, we have a lot of input and power over what our digital and work worlds looks like, and therefore can act as agents of change and repair.

John Burn-Murdoch

John Burn-Murdoch is the Financial Times’ senior data visualisation journalist, and creator of the FT’s coronavirus trajectory tracker charts. He has been leading the FT’s data-driven coverage of the pandemic, exploring its impacts on health, the economy and wider society. When pandemics are not happening, he also uses data and graphics to tell stories on topics including politics, economics, climate change and sport, and is a visiting lecturer at the London School of Economics.

John will discuss the lessons he’s learned reporting on and visualising the pandemic, including the world of difference between making charts for a technical audience and making charts for a mass audience. You’ll learn from his experience navigating the highly personal and political context within which people consume and evaluate graphics and data, and how that can help us better design and communicate with visualisations down the pipeline for the future.

Hadley Wickham

Hadley is Chief Scientist at RStudio, a member of the R Foundation, and Adjunct Professor at Stanford University and the University of Auckland. He builds tools (both computational and cognitive) to make data science easier, faster, and more fun. His work includes packages for data science (the tidyverse: including ggplot2, dplyr, tidyr, purrr, and readr) and principled software development (roxygen2, testthat, devtools, pkgdown).

Hadley will talk about how the tidyverse has evolved since its creation (just five years ago!). You’ll learn about our greatest successes, learn from our biggest failures, and get some hints of what’s coming down the pipeline for the future.

Registration for rstudio::global(2021) will open in early December

If you would like to receive notifications about the details, please subscribe below.


To leave a comment for the author, please follow the link and comment on their blog: RStudio Blog.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The post rstudio::global(2021) first appeared on R-bloggers.

This posting includes an audio/video/photo media file: Download Now

Comments