Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

> 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?

>Solution :

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(.)).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading