Skip to content

Importing data and using add_level has weird syntax. #165

@aaronrudkin

Description

@aaronrudkin

Current CRAN version

# Example pre-existing data
df = data.frame(x = rnorm(100, 10, 2), y = 1:100)

# Works, correctly:
fabricate(
    df,
    z = x - dplyr::lag(x, 1)
)

# Does not work: Data doesn't import
fabricate(
  times = add_level(
    data = df,
    z = x - dplyr::lag(x, 1),
  )
)

# Does not work: needs an N
fabricate(
  data = df,
  times = add_level(
    z = x - dplyr::lag(x, 1),
  )
)

The error is that N resolves to NULL, which is invalid, but you can see in debug that earlier than that, the data doesn't import. This is because add_level isn't designed to bring in data.

You can do it this way:

fabricate(
  data = df,
  times = add_level(
    N = 1,
    z = x - dplyr:::lag(x, 1)
  )
)

Basically, nest a level "under" the data, but make it 1 obs for every imported obs, so the level is largely illusory. This is pretty awkward syntax, and it also runs into trouble when you want to, for instance, import DF1 as level A, then mutate it to add variables, then import DF2 as non-nested level B, then mutate THAT to add variables.

This is as much a note to self as anything, but basically:

  • Documentation should have an example of the current cumbersome syntax.
  • Eventually, it should be easier to import data within an add_level(nest = FALSE) command.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions