verajosemanuel / tidyviz

Dockerfile for rich reporting and interactive visualization. base is tidyviz-base
6 stars 0 forks source link

Timestamps of installed packages in container? #1

Open pgensler opened 7 years ago

pgensler commented 7 years ago

Hello,

I'm curious how you were able to implement time stamps in your Build logs for this container such as:

Goodbye at Mon Oct 23 19:00:22 2017

Did you purposefully intend to do so? I've been meaning to try and do something similar for a container I am working on. Thanks

verajosemanuel commented 7 years ago

Yes. I did it on purpose. Take a look at the base container this depends on: tidyviz-base There's a install_github file. You'll find some useful snippets there. One of them is for timestamp log.

Hope this helps

pgensler commented 7 years ago

It does, thanks. I was trying to modify your code so that I could see when a package started, and completed install, and this is what I have come up with:

I'm wondering if this is a good use case for the glue package to combine the strings package name, along with when execution is complete for a function?


if (!require("pacman")) install.packages("pacman","glue")
pacman::p_load(pathological)

if (!file.exists("~/.Rprofile"))
  # only create if not already there
  file.create("~/.Rprofile")

perfil <- pathological::r_profile()

cat(
  .Last = function() {
  cond = suppressWarnings(!require(fortunes, quietly=TRUE))
  if(cond)
  try(install.packages('fortunes'), silent=TRUE)
  message('Package install completed for package',{x},'at',date(), '\n')
  }
  # aliases
  s <- base::summary
  h <- utils::head
  n <- base::names
  .First <- function(){
  x <- library(fortunes)
  glue::('Starting Install for package'{x})
  }
  , file = perfil, append=TRUE, sep = "\n")
verajosemanuel commented 7 years ago

I've noticed your call to install glue outside pacman call. Better this way:

if (!require("pacman")) install.packages("pacman")
pacman::p_load(pathological, glue)

Also, you don't need all the code for your purpose:


if (!file.exists("~/.Rprofile"))
  # only create if not already there
  file.create("~/.Rprofile")

myprofile <- pathological::r_profile()

cat(
  .Last = function() {
  cond = suppressWarnings(!require(fortunes, quietly=TRUE))
  if(cond)
  try(install.packages('fortunes'), silent=TRUE)
  message('Package install completed for package',{x},'at',date(), '\n')
  }

  .First <- function(){
  x <- library(fortunes)
  glue::('Starting Install for package'{x})
  }
  , file = myprofile, append=TRUE, sep = "\n")

Hope this works

pgensler commented 7 years ago

Hmm, I'm not exactly sure how glue works, as that last line was causing me errors, but this seems to work on startup:

.Last = function() {
  cond = suppressWarnings(!require(fortunes, quietly=TRUE))
  if(cond)
    try(install.packages('fortunes'), silent=TRUE)
  message('Package install completed for package',{x},'at',date(), '\n')
}

.First <- function(){
  x <- library(fortunes)
  message('Starting Install for package',{x})
}

which produces:

Starting Install for packagefortunesmethodsbase

But if I call install.packages("tidytext"), shouldn't I expect the .First and .last calls to trigger, or does that only trigger at the beginning of an R Session?

verajosemanuel commented 7 years ago

.First( ) will be run at the start of the R session and .Last( ) will be run at the end of the session.

pgensler commented 7 years ago

@verajosemanuel so is it possible to have a .Last() call trigger after the install.packages completes, or is it better to simply create a wrapper for install.packages, similar to pacman::p_install

If I understand your script, it seems like fortunes is simply a placeholder for your package, and when the function completes, it should output a message?

verajosemanuel commented 7 years ago

Fortunes is a package showing random sentences on console. My script shows message only for packages you add to the script itself. Otherwise if you need to show a message after installation of any package anytime you need to call a wrapper.

pgensler commented 7 years ago

I understand what the fortunes package does, but isen't that a placeholder? Like these lines from your build file:

  installing source* package ‘StanHeaders’ ...   package ‘StanHeaders’ successfully unpacked and MD5 sums checked   libs  ar: creating ../lib/libStanHeaders.a  installing via 'install.libs.R' to /usr/local/lib/R/site-library/StanHeaders   inst   help  * installing help indices   building package indices  * testing if installed package can be loaded   DONE (StanHeaders)  Goodbye at Tue Oct 31 21:37:16 2017

How is stanheaders using the .First and .Last call if you diden't explicitly define install.packages(rstan)? Aren't you kinda wrapping install.packages right at the installation process? Thanks for your help with this.

verajosemanuel commented 7 years ago

that is very odd, didn't notice this. I've to assume this behavior is due to some hidden call to .Last even if you don't make it explicit. I'll have to test it.

Thanks for reporting

pgensler commented 7 years ago

@verajosemanuel No problem, it's just that I'd really like to create some sort of way of stating that the package install started for package x, and completed for package x. That would be incredibly helpful.

pgensler commented 7 years ago

FYI it looks like devtools::install_cran has this functionality, but you need to supply the packages as a character vector

verajosemanuel commented 7 years ago

Have you looked at pacman functions? maybe any is suitable for you. https://cran.r-project.org/web/packages/pacman/vignettes/Introduction_to_pacman.html

pgensler commented 7 years ago

pacman does not support this natively, but I would love it if it did. You should try devtools::install_cran to see if that outputs something cleaner for your build. Any particular reason why you are installing some packages from GitHub, and not just from source?

verajosemanuel commented 7 years ago

just because the time i've included in the dockerfile the only option was github, and had no time to check it all...

pgensler commented 7 years ago

I see. It might be worth trying to do a simple unit test via testthat's tool to view what packages installed successfully: Nice output of what installed, and what did not. https://twitter.com/dvaughan32/likes