Skip to contents

[Stable]

Usage

chunks_deep_clone(chunks = get_chunks_object())

Arguments

chunks

optional, (chunks) object. If not provided then automatic chunks object detection is run via get_chunks_object

Value

a deep copy of chunks

Details

use this function if you need to copy a chunks object as this function makes sure all associated environments are deep copied (i.e. are independent of the original object)

Examples

x_chunk <- chunks_new()
chunks_push(chunks = x_chunk, expression = expression(y <- 1))

# A copy of x_chunk which does not share the same environment
x_chunk_copy <- chunks_deep_clone(x_chunk)

# Add expression only into x_chunk
chunks_push(chunks = x_chunk, expression = expression(y <- 2 * y))

# Get R code from both chunks, note it is different
chunks_get_rcode(x_chunk)
#>                  chunk_1                  chunk_2 
#>     "expression(y <- 1)" "expression(y <- 2 * y)" 
chunks_get_rcode(x_chunk_copy)
#>              chunk_1 
#> "expression(y <- 1)" 

# Evaluate both chunks, note the result is different
chunks_safe_eval(x_chunk)
#> [1] 2
chunks_safe_eval(x_chunk_copy)
#> [1] 1