Error when using knitr with renv with a Rmd file in a project subdirectory

Hi, I'm a new user to renv. Currently, I have a project organized with a "Rmarkdown" directory where all my R markdown files are stored. When I create a "Test.Rmd" file just by going to File-->"New File"-->"R markdown" it runs if I store this file at the top level directory (where my renv directory and lock file reside), but if I create this file within my Rmarkdown subdirectory and try to knit the file, I get the error below. Can you help me fix the problem?

Error in file(filename, "r", encoding = encoding) :
cannot open the connection
Calls: source -> file
In addition: Warning message:
In file(filename, "r", encoding = encoding) :
cannot open file 'renv/activate.R': No such file or directory
Execution halted


I believe this is because a .Rprofile is found which try to load the renv file as in any renv project. OI would like to know better how you project is organized to try reproduce this.

Can you share the structure of your project directory ?
fs::dir_tree() can be an interesting outputs if not too long.

By "knit the file", you mean with the Knit button right ?
does rmarkdown::render() on Rmd file is working in R console ?


My fs::dir_tree() output was ~1000 lines long. I tried to save output as a text file and upload by I got the error message saying new users can't upload attachments. But I can offer two things.

  1. This project is in github, so I point you to the current branch I'm working in, where I'm trying to set up renv (link here)
  2. At the end of this post is the most relevant part of fs::dir_tree()

By "knit the file" I meant I just click on the "knit" button in Rstudio. That's what gives the error above. Actually, I just tried the command rmarkdown::render("Rmarkdown/Test.Rmd") and that works great and already partially fixes the problem. I would like to make use of the GUI and the knit button though if possible. I see there is an option to change the "Knit Directory." Would that help me? Or would something else?

Already this was a big help, thanks a lot!

├── Rmarkdown
│ ├── 000_render_site.R
│ ├── 000_render_site_clear_cache.R
│ ├── 000_render_site_clear_cache_install_xgxr.R
│ ├── Adverse_Events.Rmd
│ ├── Data.cpp
│ ├── Data_Checking.Rmd
│ ├── Datasets.Rmd
│ ├── GuidingPrinciples.Rmd
│ ├── Makevars
│ ├── Multiple_Ascending_Dose_PD_binary.Rmd
│ ├── Multiple_Ascending_Dose_PD_binary_cache
│ │ └── html
│ │ ├── packages
│ │ ├── unnamed-chunk-3_38236da501ba13ae83b4f362dc1fdd57.RData
│ │ ├── unnamed-chunk-3_38236da501ba13ae83b4f362dc1fdd57.rdb
│ │ ├── unnamed-chunk-3_38236da501ba13ae83b4f362dc1fdd57.rdx
│ │ ├── unnamed-chunk-4_69faebd1896a157eb2ce904e8d746756.RData
│ │ ├── unnamed-chunk-4_69faebd1896a157eb2ce904e8d746756.rdb
│ │ ├── unnamed-chunk-4_69faebd1896a157eb2ce904e8d746756.rdx
│ │ ├── unnamed-chunk-5_96859af093b06a99af06b953c78e5d13.RData
│ │ ├── unnamed-chunk-5_96859af093b06a99af06b953c78e5d13.rdb
│ │ ├── unnamed-chunk-5_96859af093b06a99af06b953c78e5d13.rdx
│ │ ├── unnamed-chunk-6_ee3d29afd75c91c7e1fe9d1ff1ba7583.RData
│ │ ├── unnamed-chunk-6_ee3d29afd75c91c7e1fe9d1ff1ba7583.rdb
│ │ ├── unnamed-chunk-6_ee3d29afd75c91c7e1fe9d1ff1ba7583.rdx
│ │ ├── unnamed-chunk-7_e74296c9435515f0f8043f74b5f3c01b.RData
│ │ ├── unnamed-chunk-7_e74296c9435515f0f8043f74b5f3c01b.rdb
│ │ ├── unnamed-chunk-7_e74296c9435515f0f8043f74b5f3c01b.rdx
│ │ ├── unnamed-chunk-8_fd29143987b66684254328c2afae7ef7.RData
│ │ ├── unnamed-chunk-8_fd29143987b66684254328c2afae7ef7.rdb
│ │ └── unnamed-chunk-8_fd29143987b66684254328c2afae7ef7.rdx
│ ├── Multiple_Ascending_Dose_PD_continuous.Rmd
│ ├── Multiple_Ascending_Dose_PD_count.Rmd
│ ├── Multiple_Ascending_Dose_PD_ordinal.Rmd
│ ├── Multiple_Ascending_Dose_PD_real_example.Rmd
│ ├── Multiple_Ascending_Dose_PD_receptor_occupancy.Rmd
│ ├── Multiple_Ascending_Dose_PD_time_to_event.Rmd
│ ├── Multiple_Ascending_Dose_PK.Rmd
│ ├── Multiple_Ascending_Dose_PKPD_binary.Rmd
│ ├── Multiple_Ascending_Dose_PKPD_continuous.Rmd
│ ├── Multiple_Ascending_Dose_PKPD_count.Rmd
│ ├── Multiple_Ascending_Dose_PKPD_ordinal.Rmd
│ ├── Multiple_Ascending_Dose_PKPD_receptor_occupancy.Rmd
│ ├── Multiple_Ascending_Dose_PKPD_time_to_event.Rmd
│ ├── Multiple_Ascending_Dose_PK_KeyPlots.Rmd
│ ├── Oncology_Efficacy_Plots.Rmd
│ ├── PKPD_Datasets.Rmd
│ ├── Presentations_Publications.Rmd
│ ├── Rmarkdown.Rproj
│ ├── Single_Ascending_Dose_PK.Rmd
│ ├── SiteResources
│ │ ├── 2020_xgx_ACoP_poster.mp4
│ │ ├── 2020_xgx_ACoP_tutorial.mp4
│ │ ├── AE_vs_AUC_boxplots.png
│ │ ├── Causal_Diagram_Types.png
│ │ ├── Count_Hazard_Figure.png
│ │ ├── Event_Rate.png
│ │ ├── Kaplan_Meier.png
│ │ ├── Lab_Marker_Pct_of_ULN.png
│ │ ├── Mean_Cumulative_Function.png
│ │ ├── PD_icon.png
│ │ ├── PKPD_icon.png
│ │ ├── PK_icon.png
│ │ ├── Principles_icon.png
│ │ ├── QC_icon.png
│ │ ├── README.html
│ │ ├── RECIST.png
│ │ ├── Safety_icon.png
│ │ ├── Stein12_RAD001.png
│ │ ├── body.html
│ │ ├── header.html
│ │ ├── icon_nav.html
│ │ ├── response_category.png
│ │ └── styles.css
│ ├── Test.Rmd
│ ├── site.yml
│ ├── index.Rmd
│ ├── mod1.d
│ │ ├── Makevars
│ │ ├── mod1
│ │ ├── mod1
│ │ ├── mod1
│ │ └──
├── renv
│ ├── activate.R
│ ├── library
│ │ └── R-4.1

This seems to be related to how the IDE is running the render() command then. :thinking:
Which version of the IDE are you running ?

I'm running: RStudio 2022.02.0 Build 443

Can you try upgrading to last RStudio release at least ? or even better try the daily release ?
That would help know if this is fixed in those versions.

I'll also try to reproduce with your repo.

Ok, I just downloaded RStudio 2022.02.1 Build 461. I have the same error. Executing from the command below from the Console works, but the Knit GUI button produces the error further below.

One aside, in playing around, it's possible I once activated renv from within the Rmarkdown directory, then deleted the folders, then activated it again from the top level xgx directory. Could that have put a hidden file somewhere that knitr is now looking for when I use the GUI? (Just brainstorming)


Error in file(filename, "r", encoding = encoding) :
cannot open the connection
Calls: source -> file
In addition: Warning message:
In file(filename, "r", encoding = encoding) :
cannot open file 'renv/activate.R': No such file or directory
Execution halted


  • renv/activate.R does exist
  • Rmarkdown/renv/activate.R does not exist.

Did you change any of those settings for your document ? or your project ?

Question is: which directory is used for the Rmd ?

And also, do you have somehow the call to renv/activate.R from your user Rprofile ?

  • usethis::edit_r_profile("user") -> should not have a call to renv file
  • usethis::edit_r_profile("project") -> should have the call to renv file

Also I see you have a RStudio project inside another one.

Rmarkdown (xgx/Rmarkdown at causity1 · Novartis/xgx · GitHub) inside xgx (GitHub - Novartis/xgx at causity1) - this could create potential conflict maybe.

From which open project are you knitting the file ?


  • I cloned your repo
  • Activated the branch causity1
  • Opened xgx.Rprof
  • Opened Rmarkdown/Datasets.Rmd
  • clicked the knit button

No issue - it is rendering.

I am running very recent version (daily one)

    RStudio Edition : Desktop [Open Source]
    RStudio Version : 2022.6.0.244
    OS Version      : Windows 10 x64 (build 22000)
    R Version       : R version 4.1.2 (2021-11-01)

So it could be this, or something configured on your environment that triggers the .Rprofile

Oh shoot, yeah, I'm still confused where the Rproject should sit, whether it should be at the top level or the Rmarkdown subdirectory, and I guess I created two. Sorry for that! What I've since done is delete the xgx/Rmarkdown/Rmarkdown.Rproj file and also the xgx/Rmarkdown/.Rprofile file. Then, I reopen my xgx.Rproj project and try and Knit from the GUI and now I get a new error below. I do have htmltools 0.5.2 on my local machine, but the renv.lock file was generated on a different system which only has htmltools I don't know what htmltools is or why version 0.5.2 is needed.

Also, I didn't put the call to renv/activate.R in my .Rprofile file. I believe renv did that. Isn't it supposed to be there, so that the right renv environment is used when I load the project?

I didn't change the RStudio settings for my project. Also, I think I need the default setting, for the Knit directory to be the file directory, and not the project directory, for references to other files to work properly.

Rendering: /Users/steinanf/git/xgx/Rmarkdown/Test.Rmd
Error in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]) :
namespace ‘htmltools’ is already loaded, but >= 0.5.2 is required
Calls: ... is_bs_theme -> loadNamespace -> namespaceImport -> loadNamespace
Execution halted

Yes this is put by renv so that the renv project is loaded when you open the project or run R in this directory. It is supposed to be there, and should not create any issue.

Yes the default is best to keep.

When you opened the project, did the renv project loaded ?
I have something like this in console when it opens

* Project '~/DEV_OTHER/xgx' loaded. [renv 0.14.0]
* The project may be out of sync -- use `renv::status()` for more details.

All package in your lockfile (and so renv project) should be compatible with each other.
Be sure that your renv project is loaded, and then renv::status() and install any missing package.

Then, try again.

Yes, the lock file loads. I get this message after opening the project.

* Project '~/git/xgx' loaded. [renv 0.14.0]

And it's of htmltools that is available. (Though I do have 0.5.2 on my system as well). I see that by running this command.

[1] ‘’

And I get the error below when I knit my Rmarkdown/Test.Rmd file from the GUI. (It works from the Console).
Rendering: /Users/steinanf/git/xgx/Rmarkdown/Test.Rmd
Error in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]) :
namespace ‘htmltools’ is already loaded, but >= 0.5.2 is required
Calls: ... is_bs_theme -> loadNamespace -> namespaceImport -> loadNamespace
Execution halted

I think you need to upgrade the packages in your lockfile. Somehow the lockfile could gave been out of date.

rmarkdown imports bslib which imports htmltools and I think the second as a 0.5.2 requirement for the latter.

Can you share the version of rmarkdown and bslib that could have been installed in your renv project ?

renv::status() could show you if you have differences between lockfile and library

Shoot, my first reply seems to have disappeared, so I'm reposting.

I cannot update the packages in my lock file. I work at a large pharma company and we have a server which hosts a highly regulated and controlled R environment. I also have a local machine where I can have whatever R environment I want. I'm using renv to try and ensure my local machine R environment is as close to our server as possible (at least same version of R, package versions, and install locations).

On my local machine, renv::status() didn't seem like it gave a useful output, so to answer your question, I tried using packageVersion() instead. See below for ouputs.

The following package(s) are no longer used in this project:
packrat [0.7.0]
rsconnect [0.8.24]

Use renv::snapshot() to remove them from the lockfile.

[1] ‘2.11’
Error in packageVersion("bslib") : there is no package called ‘bslib’
[1] ‘’

On my server, we do have bslib (see below on command from server), so I guess if I need to, I could add bslib to the list of packages that renv pulls in, if you think I should. Right now I do it by just having a dummy Rmd file that calls these packages for no reason, other than to be pulled into renv. If that's what I should do and there's a better way to do it, let me know!
R 4.1.0> packageVersion("bslib")
[1] ‘’

This seems ok to me if you don't have bslib in your project

With rmarkdown 2.11, bslib was only suggested with min

and version of bslib was requirement htmltools 0.5.1

I don't quite understand why bslib would be loaded and requiring htmltools in higher version when you are rendering. It is as if the renv project is not used during rendering. :thinking:

I'll try to find if the IDE version you are using as some requirement on package version that does not meet your renv lockfile. It can happens.

Edit: no it seems the verison of the IDE you are using don't have higher requirement...

For now, I can't reproduce using the github repo. I'll try to start a project with rmarkdown and renv from scratch.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.