R function that works with NSE and Shiny Inputs - r

I am looking for an easy way to have my function work with input the comes from Shiny (i.e. string input) or with typical interactive use that is Tidyverse functions enable with NSE. Without the need to duplicate my code to handle each case separately.
An example of usage:
library(dplyr)
flexible_input <- function(var){
mtcars %>%
select(var)
}
# This works for NSE
nse_input <- function(var){
mtcars %>%
select({{ var }})
}
# This works for shiny but now I am duplicated my code essentially
shiny_input <- function(var){
mtcars %>%
select(.data[[var]])
}
flexible_input(mpg)
flexible_input('mpg')

If we need flexible_input to take string and unquoted input, convert to symbol and evaluate (!!)
flexible_input <- function(var){
mtcars %>%
dplyr::select(!! rlang::ensym(var))
}
-testing
> flexible_input(mpg) %>% head
mpg
Mazda RX4 21.0
Mazda RX4 Wag 21.0
Datsun 710 22.8
Hornet 4 Drive 21.4
Hornet Sportabout 18.7
Valiant 18.1
> flexible_input("mpg") %>% head
mpg
Mazda RX4 21.0
Mazda RX4 Wag 21.0
Datsun 710 22.8
Hornet 4 Drive 21.4
Hornet Sportabout 18.7
Valiant 18.1

Related

Get value based on another column in dplyr

I have the following dataset:
df <- mtcars[1:4,c("wt","qsec")]
df
wt qsec
Mazda RX4 2.620 16.46
Mazda RX4 Wag 2.875 17.02
Datsun 710 2.320 18.61
Hornet 4 Drive 3.215 19.44
How to achieve the following by using dynamic variable via dplyr?
df %>%
mutate(wt=floor(wt[which.min(qsec)]))
This is what I tried so far:
myvar<-"wt"
df %>%
mutate(!!myvar :=floor(!!as.name(myvar)[which.min(qsec)]))
Error in which.min(qsec) : object 'qsec' not found
Please let me know if you know why does the above code failed. Thank you!
In the latest versions of dplyr, you use := to set names with a character value and you use .data[[]] to get columns with a character value. Your transformation would look like this
df %>% mutate("{myvar}" := floor(.data[[myvar]][which.min(qsec)]))

How to get output with the same prefix than parameter function

I want to get the input parameter of a function to create an output with the same prefix in the global env
fun_mtcars <- function(name_ref,...){
df <- name_ref %>%
select(mpg,...)
.GlobalEnv$selec_name_ref <- df
}
fun_mtcars(mtcars,disp)
In global env a new data frame was created with the name "selec_name_ref " but I want a name "selec_mtcars"
I can do selec_mtcars <- fun_mtcars(mtcars,disp)
but I have a lot of function to execute
We may extract the object name as a string with deparse/substitute and use that in paste to create new object and assign to .GlobalEnv with [[ instead of $
fun_mtcars <- function(name_ref,...){
name_ref_str <- deparse(substitute(name_ref))
df <- name_ref %>%
select(mpg,...)
.GlobalEnv[[paste0("select_", name_ref_str)]] <- df
}
-checking
fun_mtcars(mtcars,disp)
> head(select_mtcars)
mpg disp
Mazda RX4 21.0 160
Mazda RX4 Wag 21.0 160
Datsun 710 22.8 108
Hornet 4 Drive 21.4 258
Hornet Sportabout 18.7 360
Valiant 18.1 225

Function in a dplyr pipeline executes the first item repeatedly rather than one for each

I have a table full of data, where one of the columns is scraped from a webpage and thus is full of HTML tags I don't need. I was looking to remove the HTML tags. I found this thread: Removing html tags from a string in R
I eventually got the regex version working (so my actual problem is solved), but originally tried implementing David Robinson's answer which utilised the rvest package. However, when I tried that, I had an issue where instead of running the function across each table row's string, it just performed it on the first row and copied the result down. I'm curious as to what I was doing wrong, so I know how to fix my calls next time I hit this kind of issue. Here's an example:
library(dplyr)
library(tibble)
library(rvest)
mtcars %>%
rownames_to_column("Car") %>%
select(Car) %>%
mutate(html_string = paste0("<a>",Car,"</a>")) %>%
mutate(cleaned_string = html_text(read_html(html_string)))
#thelatemail is correct I believe, read_html works only for single url, for it to work for multiple url's you need to either use rowwise or use some kind of looping.
library(dplyr)
library(rvest)
mtcars %>%
rownames_to_column("Car") %>%
select(Car) %>%
mutate(html_string = paste0("<a>",Car,"</a>")) %>%
rowwise() %>%
mutate(cleaned_string = html_text(read_html(html_string)))
# Car html_string cleaned_string
# <chr> <chr> <chr>
# 1 Mazda RX4 <a>Mazda RX4</a> Mazda RX4
# 2 Mazda RX4 Wag <a>Mazda RX4 Wag</a> Mazda RX4 Wag
# 3 Datsun 710 <a>Datsun 710</a> Datsun 710
# 4 Hornet 4 Drive <a>Hornet 4 Drive</a> Hornet 4 Drive
# 5 Hornet Sportabout <a>Hornet Sportabout</a> Hornet Sportabout
# 6 Valiant <a>Valiant</a> Valiant
# 7 Duster 360 <a>Duster 360</a> Duster 360
# 8 Merc 240D <a>Merc 240D</a> Merc 240D
# 9 Merc 230 <a>Merc 230</a> Merc 230
#10 Merc 280 <a>Merc 280</a> Merc 280
# … with 22 more rows
Or using purrr::map_chr
mtcars %>%
rownames_to_column("Car") %>%
select(Car) %>%
mutate(html_string = paste0("<a>",Car,"</a>")) %>%
mutate(cleaned_string = purrr::map_chr(html_string, ~html_text(read_html(.))))

Indexing by column name to the end of the dataframe - R

I'm wondering if there is a way to select a group of columns by the name of the first column in the group and then all the next columns either a) to the end of the data frame, or b) to another column, also using its name.
a) As an example for the first question, in the mtcars dataset, is there a way to select the columns from drat to the end of the data frame? (Something like mtcars[,'drat':ncol(mtcars)])
b) For the second question, is there a way to select the columns starting at cyl and ending at wt? (Something like mtcars[,'cyl':'wt'])
Many elegant solutions already provided but one can even use base-R to get the desired result using which as:
Ans a:
mtcars[,which(names(mtcars) == "drat"):ncol(mtcars)]
Ans b:
mtcars[,which(names(mtcars) == "cyl"):which(names(mtcars) == "wt")]
# cyl disp hp drat wt
#Mazda RX4 6 160.0 110 3.90 2.620
#Mazda RX4 Wag 6 160.0 110 3.90 2.875
#Datsun 710 4 108.0 93 3.85 2.320
#Hornet 4 Drive 6 258.0 110 3.08 3.215
#Hornet Sportabout 8 360.0 175 3.15 3.440
#......so on
We can do with this with select from dplyr
Answer a)
mtcars %>% select(drat:get(last(names(.))))
Answer b)
mtcars %>% select(cyl:wt)
In dplyr, the select function does exactly this (no quotes needed):
mtcards %>%
select(cyl:wt)
If we need to use a quoted string, convert it to sym (symbol) and then do the evaluation (!!
mtcars %>%
select(!! (rlang::sym("cyl")): !!(rlang::sym("wt")))
It would be when these are stored in an object
a <- "cyl"
b <- "wt"
mtcars %>%
select(!! (rlang::sym(a)): !!(rlang::sym(b)))
Or another option is
mtcars %>%
select(!! rlang::parse_expr(glue::glue("{a}:{b}")))

use get() and eval() to pass argument in dplyr functions

I'm trying to write my function and need to pass argument inside.
Use mtcars dataset as an example:
get.param <- function(data, var){
data %>% select(eval(var)) %>%
head()
}
get.param(mtcars, 'hp')
In the above function, replacing eval() with get() gave me error.
I'm little bit confused which one should I use. I use get() i some other functions and work. What is the difference between these two?
You can get it to work via
get.param <- function(data, var){
var <- enquo(var)
data %>% select(!!var) %>%
head()
}
get.param(mtcars, hp)
hp
Mazda RX4 110
Mazda RX4 Wag 110
Datsun 710 93
Hornet 4 Drive 110
Hornet Sportabout 175
Valiant 105
Normally one does not use get or eval with dplyr. See the vignette in the rlang package for how it is done with that package; however, in this particular case one can just pass var directly to select adding parentheses around it so that it does not confuse it with a column called "var" should it exist. If you are not worried about that edge case you could omit the parentheses.
get.param <- function(data, var) {
data %>% select((var)) %>% head
}
get.param(mtcars, 'hp')
giving:
hp
Mazda RX4 110
Mazda RX4 Wag 110
Datsun 710 93
Hornet 4 Drive 110
Hornet Sportabout 175
Valiant 105
Another possibility is to use ... like this and giving the same answer. In this variation we don't need to add the parentheses to eliminate an edge case. It also allows multiple columns to be specified.
get.param <- function(data, ...) {
data %>% select(...) %>% head
}
get.param(mtcars, 'hp')

Resources