{ "version": "https://jsonfeed.org/version/1.1", "title": "blog", "expired": false, "items": [ { "id": "./gen/restart_your_r_session.html", "url": "./gen/restart_your_r_session.html", "title": "Using R: Restart your R session", "content_html": "\n\n\n \n \n \n \n \n Using R: Restart your R session\n \n \n \n\n\n« blog\n
\n

Using R: Restart your R session

\n

Martin Johnsson

\n

2023-08-06

\n
\n

Don\u2019t save your workspace

\n

A few years ago I wrote this piece of advice about using R:

\n
\n

To everyone learning R: Don\u2019t save your workspace.

\n

When you exit an R session, you\u2019re faced with the question of whether\nor not to save your workspace. You should almost never answer yes.\nSaving your workspace creates an image of your current variables and\nfunctions, and saves them to a file called \u201d.RData\u201d. When you re-open R\nfrom that working directory, the workspace will be loaded, and all these\nthings will be available to you again. But you don\u2019t want that, so don\u2019t\nsave your workspace.

\n

Loading a saved workspace turns your R script from a program, where\neverything happens logically according to the plan that is the code, to\nsomething akin to a cardboard box taken down from the attic, full of\nassorted pages and notebooks that may or may not be what they seem to\nbe. You end up having to put an inordinate trust in your old self. I\ndon\u2019t know about your old selves, dear reader, but if they are anything\nlike mine, don\u2019t save your workspace.

\n

What should one do instead? One should source the script often,\nideally from freshly minted R sessions, to make sure to always be\nworking with a script that runs and does what it\u2019s supposed to. Storing\na data frame in the workspace can seem comforting, but what happens the\nday I overwrite it by mistake? Don\u2019t save your workspace.

\n

Yes, I\u2019m exaggerating. When using any modern computer system, we rely\non saved information and saved state all the time. And yes, every time a\ncomputation takes too much time to reproduce, one should write it to a\nfile to load every time. But I that should be a deliberate choice,\nworthy of its own save() and load() calls1,\nand certainly not something one does with simple stuff that can be\nreproduced a the blink of an eye. Put more trust in your script than in\nyour memory, and don\u2019t save your workspace.

\n
\n

Restart your R session

\n

Here is a sequel to that advice: Restart your R session.

\n

It is very tempting to just press \u201cSource\u201d or \u201cRun All\u201d again in\nRStudio to run the script from the top, but be kind to yourself and\nfirst restart your R session.

\n

The logic is exactly the same as above. R invites interactive use\nwhile you are developing your scripts, which is great. You can\nexperiment, poke around in the objects, and learn while you\u2019re doing. At\nthe same time, your working environment tends to accumulate all kinds of\nbaggage. Maybe the neat function that you wrote accidentally relies on\nglobal variable that you created before encapsulating the code, and that\nis still around in your environment. Get rid of that by frequently\nrestarting your R session.

\n

Working close to the limit of your computer\u2019s ability? Seeing that\nvector memory exhausted (limit reached?)? Sure, you could\nmanually rm some of the big objects, but it\u2019s easier to\nrestart your R session.

\n

Working on a package? Trying to keep the analysis code separate from\nfunctions that can be reused? Good idea. You have rebuilt the package,\nrun the tests \u2026 it should work now. You won\u2019t know until you restart\nyour R session.

\n

Maybe you know perfectly in what order you ran those blocks of code\nin your RMarkdown, backtracking through any change and keeping track of\nthe dependencies between chunks in your head like a wizard \u2026 or you\u2019re\nlike me and you better restart your R session.

\n

Fire, fury, projects and\npackages

\n

When writing about this on Mastodon, Ben Bolker pointed me to Jenny\nBryan\u2019s post on Project-oriented\nworkflow, which includes this memorable quote, originally from one\nof her talks:

\n
\n

If the first line of your R script is

\n

setwd(\"C:\\Users\\jenny\\path\\that\\only\\I\\have\")

\n

I will come into your office and SET YOUR COMPUTER ON FIRE

\n
\n

The point of the post is that data analyses should be organised into\nproject folders (and if you\u2019re an RStudio user, Rstudio projects to aid\nthat).

\n

On Mastodon, @datamaps@social.linux.pizza also\nsaid:

\n
\n

I tend to go much further: > build your own package! << even\nif you need to store only one single dataset or function (remember that\nyou do NOT need to upload it on CRAN)

\n
\n

This is more overhead; giving every data analysis its own package is\na bit much, and I don\u2019t think R packages are an amazing way to store\ndatasets. But if you have a bunch of different data analysis projects\nand some of functions that can be shared between them, the right way\nshare those functions is probably to make a package, rather than\ncopy/pasting! Packages are not just for sharing code with others, but to\nshare code with the most important collaborator, yourself.

\n
\n
\n
    \n
  1. Today, I would probably recommend using\nsaveRDS and readRDS instead. They just let you\nsave and load one object at a time, and allows you to easily load\nsomething under a new name. This tends to be cleaner.\u21a9\ufe0e

  2. \n
\n
\n\n\n", "summary": "You really should.", "date_published": "2023-08-06" }, { "id": "./gen/research_update2023.html", "url": "./gen/research_update2023.html", "title": "Research update 2023", "content_html": "\n\n\n \n \n \n \n \n Research update 2023\n \n \n \n\n\n« blog\n
\n

Research update 2023

\n

Martin Johnsson

\n

2023-07-29

\n
\n

Seeing as it was quite a while since I last blogged about science,\nwhat has been going on since then? I suppose one can make these kinds of\nlists seem more impressive by just posting them infrequently.

\n

Genetics of bone\nquality in laying hens

\n

We have ongoing projects about bone quality in laying hens. It\u2019s a\nbig problem; there\u2019s definitely a genetic component; measurement is\ntricky; watch this space. This year, we published the results of a study\non commercial crossbred hens, which I posted about back\nwhen it was a preprint.

\n
\n

Johnsson M, Wall H, Lopes Pinto FA, Fleming RH, McCormack HA,\nBenavides-Reyes C, Dominguez-Gasca N, Sanchez-Rodriguez E, Dunn IC,\nRodriguez-Navarro AB, Kindmark A, de Koning D-J. (2023) Genetics of tibia bone\nproperties of crossbred commercial laying hens in different housing\nsystems. G3.

\n
\n

Furthermore, Mohammed Sallam\npublished the first paper of his PhD, about genetic mapping of bone\ncomposition in a pure line. Sallam is a PhD student with Dirk-Jan de\nKoning, and I\u2019m one the co-supervisors.

\n
\n

Sallam M, Wilson PW, Andersson B, Schmutz M, Benavides C,\nDominguez-Gasca N, Sanchez-Rodriguez E, Rodriguez-Navarro AB, Dunn IC,\nde Koning DJ, Johnsson M. (2023) Genetic markers\nassociated with bone composition in Rhode Island Red laying hens.\nGenetics Selection Evolution.

\n
\n

Pig genomics with\nwhole-genome sequence data

\n

When I was a postdoc in John Hickey\u2019s group at the Roslin Institute,\nI took part in the efforts to collect, impute, and analyse whole-genome\nsequence data in a pig breeding program. The centerpieces of this work,\nled by Roger Ros-Freixedes, now at Lleida, were published last year:

\n
\n

Ros-Freixedes R, Johnsson M, Whalen A, Chen CY, Valente BD, Herring\nWO, Gorjanc C, Hickey JM. (2022) Genomic prediction\nwith whole-genome sequence data in intensely selected pig lines.\nGenetics Selection Evolution.

\n
\n
\n

Ros-Freixedes R, Valente BD, Chen CY, Herring WO, Gorjanc G, Hickey\nJM, Johnsson M. (2022) Rare and\npopulation-specific functional variation across pig lines. Genetics\nSelection Evolution.

\n
\n

John left the Roslin, but there\u2019s still an ongoing project to use\nthese sequence data for genetic mapping and fine-mapping. This year, we\npublished a genome-wide association study that used sequence data and\npCADD scores to nominate candidate variants:

\n
\n

Desire S, Johnsson M, Ros-Freixedes R, Chen C-Y, Holl JW, Herring WO,\nGorjanc G, Mellanby RJ, Hickey JM, Jungnickel MK. (2023) A genome-wide\nassociation study for loin depth and muscle pH in pigs from intensely\nselected purebred lines. Genetics Selection Evolution.

\n
\n

We also had reasons to look into the genome of the classic PK15 cell\nline; a sneak peek was published as a WCGALP conference\ncontribution:

\n
\n

Johnsson M, Hickey JM, Jungnickel MK. (2022) Building in vitro\ntools for livestock genomics: chromosomal variation within the PK15 cell\nline. Proceedings of the World Congress on Genetics Applied to\nLivestock Production.

\n
\n

The genome dynamics project

\n

Our project about features of the genome and diversity in livestock\nis in full swing. We published some preliminary work as a WCGALP\nconference contribution:

\n
\n

Johnsson M, Johansson AM, Klingstr\u00f6m T, Rius-Vilarrasa E. (2022) The effect of\nrecombination rate on genomic selection in simulation. Proceedings\nof the World Congress on Genetics Applied to Livestock Production.

\n
\n

I will be talking more about this stuff at EAAP. If you\u2019re\ninterested, maybe see you there?

\n

Deleterious\nvariants under balancing selection

\n

Colleagues at the department have been looking into the fragile foal\nsyndrome variant, where a recessive allele causes a very serious\ncollagen defect in horses. It\u2019s one of those conspicously common\nrecessive lethals. We got together to analyse data and make some models\nto see wheter it is subject to balancing selection (it seems like it)\nand whether one could use a genotyping to select against it\n(tentatively, yes).

\n
\n

Ablondi M, Johnsson M, Eriksson S, Sabbioni A, Viklund \u00c5G, Mikko S.\n(2022) Performance\nof Swedish Warmblood fragile foal syndrome carriers and breeding\nprospects. Genetics Selection Evolution.

\n
\n

Opinions on\nanimal genomics (boy do I have them)

\n

Finally, I\u2019ve published (preprinted) a couple of rather opinionated\npapers about genomics in animal breeding. The first presents two\ndifferent ways to think about the genome, that I think animal\ngeneticists use, that come with two different ways to think about\ngenomics in animal breeding. It was stimulated by a seminar held at the\nInstitute for Advanced Studies in the Humanities in Edinburgh a few\nyears ago, but most of the writing is much newer; sometimes ideas need a\ncouple of years to incubate.

\n
\n

Johnsson M. (2023) Genomics in animal\nbreeding from the perspectives of matrices and molecules.\nHereditas.

\n
\n

Also, Hereditas is the journal of the Mendelian Society of Lund,\nwhich I believe makes it the only Swedish (-ish; it\u2019s run by Springer\nNature as part of the BMC family), so publishing there is a bit of a\nbucket list item.

\n

The second is a preprint about whole-genome sequence data in genomic\nprediction, stimulated by an invitation to write something for a special\nissue about the big issues in livestock genomics.

\n
\n

Johnsson M. (2023) The big challenge for\nlivestock genomics is to make sequence data pay. ArXiv preprint.

\n
\n
\n\n
A group of Gute sheep looking curious in\nthe evening. (Photo: Martin Johnsson. License: cc-by\n4.0)
\n
\n\n\n", "summary": "What have I been up to?", "date_published": "2023-07-29" }, { "id": "./gen/new_year_new_blog.html", "url": "./gen/new_year_new_blog.html", "title": "New year, new blog, new title", "content_html": "\n\n\n \n \n \n \n \n New year, new blog, new title\n \n \n \n\n\n« blog\n
\n

New year, new blog, new title

\n

Martin Johnsson

\n

2023-07-25

\n
\n

This will be the new home of my blog (previously here). I\u2019ve\nbeen blogging on and off since 2010, as long as\u2013or possibly a little\nlonger than\u2013I\u2019ve been involved in research. Supposedly, science blogs\nare a dead medium, but I still like them and wish more scientists would\ntake the time to write them.

\n

Recently, the blog has been on an extended unplanned hiatus. This is\nnot the first time I\u2019ve slacked off, and some of the reasons are\nobvious. When things get busy, writing for fun is one of the first\nthings to go. However, another reason is the friction of the medium.\nI\u2019ve grown less and less fond of Wordpress and its hopelessly painful\nuser interface.

\n

pandoc to the rescue

\n

In the search for something nice and minimalist, I found Lukas\nSchwab\u2019s \u201cvery basic Pandoc static site generator\u201d (pandoc-blog on\nGitHub), that uses the Swiss army document converter pandoc to turn a folder of\nMarkdown files into a series of html pages presented in chronological\norder\u2013i.e., a blog.

\n
\n

Deeply unattractive out of the box? Yes. Easy to customize? I hope\nso.

\n
\n

Sounds great to me! This means losing some of the fancy\nfunctionality, obviously. The options for subscribing are limited, and\nthere\u2019s no comments section. But then again, who comments on blogs in\n2023? The 2000s called and wanted back their futile hope for a\ndemocratic online future! Please send me an email instead.

\n

With some luck, this will feel better. Eventually I might figure out\nhow to point onunicornsandgenes.blog to\nthis page instead. The old posts will remain on Wordpress, because I\ncan\u2019t be asked to convert them.

\n

Docent appointment

\n

Also, I was recently appointed docent (associate professor1) in Animal Science, specialisation\nin Quantitative Genetics and Genomics at the Swedish University of\nAgricultural Sciences. For any reader not hip to what that means, it\nmeans I can be the main supervisor of PhD students, participate in PhD\nexamination committees, and that I have another official document\ncertifying that I am, indeed, a huge geek.

\n
\n\n
A Swedish Red Polled cow eating leaves\nunder a blue sky. (Photo: Martin Johnsson. License: cc-by\n4.0)
\n
\n
\n
\n
    \n
  1. The translation of the title into English is not that\nclear-cut, but \u201cassociate professor\u201d is the one that SLU goes with.\u21a9\ufe0e

  2. \n
\n
\n\n\n", "summary": "This will be the new home of Martin's blog.", "date_published": "2023-07-25" } ] }