LulY
LulY

Reputation: 1294

Why does deparse return a vector of length two here?

I need to deparse within R using deparse(...) which in all cases returned a vector of length one. But for the following I get a vector of length two:

> deparse(factor(All_cause_death, levels= c('Dead', 'Alive')) ~ Age_in_years + Systolic_Blood_pressure, backtick= TRUE)

[1] "factor(All_cause_death, levels = c(\"Dead\", \"Alive\")) ~ Age_in_years + "
[2] "    Systolic_Blood_pressure" 

If I shorten the first variable name a bit I indeed get the expected output of length one:

> deparse(factor(Death, levels= c('Dead', 'Alive')) ~ Age_in_years + Systolic_Blood_pressure, backtick= TRUE)
[1] "factor(Death, levels = c(\"Dead\", \"Alive\")) ~ Age_in_years + Systolic_Blood_pressure"

In the code above I shortened the input by 10 elements by changing "All_cause_death" to "Death". Interestingly, if I shorten the input by 10 elements (or more) at the end of the input I do not get vector of length one:

> deparse(factor(All_cause_death, levels= c('Dead', 'Alive')) ~ Age_in_years + Systolic_, backtick= TRUE)
[1] "factor(All_cause_death, levels = c(\"Dead\", \"Alive\")) ~ Age_in_years + "
[2] "    Systolic_"

Why is this happening and how can I get a vector of length one for the first code?

Upvotes: 0

Views: 178

Answers (1)

Allan Cameron
Allan Cameron

Reputation: 173803

deparse will break across lines if they are longer than 60 characters. It has the width.cutoff parameter which you can set to a longer length if you prefer this not to happen:

deparse(factor(All_cause_death, levels= c('Dead', 'Alive')) ~ Age_in_years+ Systolic_Blood_pressure, backtick= TRUE, width.cutoff = 200)
#> [1] "factor(All_cause_death, levels = c(\"Dead\", \"Alive\")) ~ Age_in_years + Systolic_Blood_pressure"

From the deparse documentation:

width.cutoff integer in [20, 500] determining the cutoff (in bytes) at which line-breaking is tried.

You can use also use deparse1 instead of deparse, which effectively just sets the maximum width.cutoff to 500 by default

deparse1(factor(All_cause_death, levels= c('Dead', 'Alive')) ~ Age_in_years+ Systolic_Blood_pressure, backtick= TRUE)
#> [1] "factor(All_cause_death, levels = c(\"Dead\", \"Alive\")) ~ Age_in_years + Systolic_Blood_pressure"

From the documentation for deparse1:

deparse1() is a simple utility added in R 4.0.0 to ensure a string result (character vector of length one), typically used in name construction, as deparse1(substitute(.)).

Upvotes: 2

Related Questions