using inputs within mutate

For my project, there are three "types" of values for a given dimension from which the user can choose for display. The way I implement this is that data has 4 columns for that value: one each for the three options, and one "in play" which is overwritten with the values from the column selected by the user. The code I'm using for this looks like this:

df$DISPLAYCOLUMN<- df[[input$USERCHOSENDATACOLUMN]]

Can this be translated into mutate ? Something like:

df %>% mutate( DISPLAYCOLUMN = input$USERCHOSENDATACOLUMN )
1 Like

Short Answer:

Yes, quite easily, simply adjust the syntax to incorporate rlang's .data and (optional) .env pronouns:

require(dplyr)
require(rlang)

new_df <- df |>
  dplyr::mutate(
    DISPLAYCOLUMN = .data[[input$USERCHOSENDATACOLUMN]]
  )

This allows you to replace df[[input$USERCHOSENDATACOLUMN]] with .data[[input$USERCHOSENDATACOLUMN]]. Just note that .data depends on the rlang library (see below for longer answer).

Long Answer:

The concept you are referring to here is a prominent component in the tidyverse and used to be quite complicated but has become much more manageable over the years: Tidy Evaluation.

Essentially it is how tidyverse packages are allowed to have unquoted arguments passed to them referencing actual column names or encapsulated objects that need to be evaluated before being used.

It is very beneficial to review the Tidy Evaluation (formerly dplyr's "Programming with dplyr" Vignette") a couple times to understand the concept of encapsulation, quosures, quoting, unquoting, symbols, names, the !! "bang-bang" operator and its predecessor unquo(), etc. This will take your low-level R knowledge to the next level.

Resources:


Back to your specific question and rlang:

By utilizing the principles of "tidy evaluation" (see R/eval-tidy.R from rlang), along with rlang's provided pronouns when programming with data-masked functions, one can use the pronouns as such:

  • .data: retrieves data variables from the active data. Frame in the pipeline.
  • .env: retrieves env-specific variables from the environment at the same scope of the current session.

Because the lookup is explicit, there is no ambiguity between both kinds of variables:

myvar <- 10
mtcars |> dplyr::mutate(disp = .data$disp * .env$disp)
mtcars |> dplyr::mutate(disp = disp * disp)

Notes:

  • .data is simply a pronoun and not the actual data. Frame. This means that you can't take its names or map a function over the contents of .data . Similarly, .env is not an actual R environment. For instance, it doesn't have a parent and the sub-setting operators behave differently.*

  • If you are working on a package be sure to import .data and .env to the package NAMESPACE from rlang to avoid R CMD Check issues. You should also declare the global variable via utils::globalVariables(c(".data", ".env")).

  • rlang::.data is a "fake" pronoun. Do not refer to rlang::.data with the rlang:: prefixed qualifier in data masking code. Use the unqualified .data symbol that is automatically put in scope by data-masking functions.

This all falls under the umbrella of the Tidy Evaluation Framework (r-lib.org).

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.