business-science / tidyquant

Bringing financial analysis to the tidyverse
https://business-science.github.io/tidyquant/
Other
854 stars 175 forks source link

Error in as.POSIXlt(x, tz = tz(x)) : error during cleanup #62

Closed verajosemanuel closed 7 years ago

verajosemanuel commented 7 years ago

Got a very simple Rmd notebook (the demo offered when clicking on menu File > New File > Rnotebook. Just added code for loading tidyquant package. When knitting to HTML Rstudio markdown pane gives me:

Output created: report.html
Error in as.POSIXlt(x, tz = tz(x)) : 
  argument "x" absent, with no default
Calls: .Last -> message -> date -> date.default -> as.POSIXlt
Execution interrupted

Tried to render in putty terminal accessing to server:

r -e "rmarkdown::render('/home/user/R/reporting/report.Rmd')"

Terminal gets cluttered with error and must kill process:


    Error in as.POSIXlt(x, tz = tz(x)) :
      el argumento "x" está ausente, sin valor por omisión
    Fatal error: error during cleanup

    Error in as.POSIXlt(x, tz = tz(x)) :
      el argumento "x" está ausente, sin valor por omisión
    Fatal error: error during cleanup

    Error in as.POSIXlt(x, tz = tz(x)) :
      el argumento "x" está ausente, sin valor por omisión
    Fatal error: error during cleanup
......
....
...

Had to comment library(tidyquant) to get sessionInfo

## R version 3.4.1 (2017-06-30)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 16.04.3 LTS
## 
## Matrix products: default
## BLAS: /usr/lib/libblas/libblas.so.3.6.0
## LAPACK: /usr/lib/lapack/liblapack.so.3.6.0
## 
## locale:
##  [1] LC_CTYPE=es_ES.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=es_ES.UTF-8        LC_COLLATE=es_ES.UTF-8    
##  [5] LC_MONETARY=es_ES.UTF-8    LC_MESSAGES=es_ES.UTF-8   
##  [7] LC_PAPER=es_ES.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=es_ES.UTF-8 LC_IDENTIFICATION=C       
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## loaded via a namespace (and not attached):
##  [1] compiler_3.4.1  backports_1.0.5 magrittr_1.5    rprojroot_1.2  
##  [5] tools_3.4.1     htmltools_0.3.6 yaml_2.1.14     Rcpp_0.12.12   
##  [9] stringi_1.1.5   rmarkdown_1.6   knitr_1.17      stringr_1.2.0  
## [13] digest_0.6.12   evaluate_0.10.1
DavisVaughan commented 7 years ago

@verajosemanuel can you post a small reproducible example of this? I'm not sure what is happening here. Why is as.POSIXlt being called?

verajosemanuel commented 7 years ago

File > New File > Rnotebook. Just added code for loading tidyquant package this way:


---
title: "R Notebook"
output: html_notebook
---

This is an [R Markdown](http://rmarkdown.rstudio.com) Notebook. When you execute code within the notebook, the results appear beneath the code. 

Try executing this chunk by clicking the *Run* button within the chunk or by placing your cursor inside it and pressing *Cmd+Shift+Enter*. 

```{r}
library(tidyquant)
plot(cars)

Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Cmd+Option+I.

When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the Preview button or press Cmd+Shift+K to preview the HTML file).

The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike Knit, Preview does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.

DavisVaughan commented 7 years ago

Hmm, I can render this just fine. I rendered using RStudio's Knit to HTML button and from the command line using Terminal. Both worked for me.

Which version of tidyquant are you running? I would try a clean install using install.packages(tidyquant). I ran with 0.5.3 and it worked.

Also make sure that pandoc is up to date. On Mac I can update with brew install pandoc. I think at least version 1.12.3 is required to render, although I doubt this is the problem.

I'm most interested in why as.POSIXlt() is trying to do anything. tidyquant doesn't do anything like that when its attached. Can you provide any more information on this from your end?

DavisVaughan commented 7 years ago

From the console in RStudio, can you library(tidyquant) without any errors?

DavisVaughan commented 7 years ago

A potentially similar issue here. This may have to do with lubridate? tidyquant does depend on lubridate (it loads lubridate when loading tidyquant) some I'm wondering if thats it.

Try and replace library(tidyquant) with library(lubridate) and try and render again.

verajosemanuel commented 7 years ago

Hi @DavisVaughan , I can load library with no issue at console. The issue only arises loading tidyquant, not lubridate.

Regards

DavisVaughan commented 7 years ago

@verajosemanuel, I just spun up my AWS Linux server with RStudio on it and tried this. It worked with no issues. My sessionInfo() was fairly similar to yours.

```r > sessionInfo() R version 3.4.0 (2017-04-21) Platform: x86_64-pc-linux-gnu (64-bit) Running under: Ubuntu 16.04.1 LTS Matrix products: default BLAS: /usr/lib/openblas-base/libblas.so.3 LAPACK: /usr/lib/libopenblasp-r0.2.18.so locale: [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=en_US.UTF-8 [4] LC_COLLATE=en_US.UTF-8 LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 [7] LC_PAPER=en_US.UTF-8 LC_NAME=C LC_ADDRESS=C [10] LC_TELEPHONE=C LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C attached base packages: [1] stats graphics grDevices utils datasets methods base loaded via a namespace (and not attached): [1] compiler_3.4.0 tools_3.4.0 ```

The last thing I would recommend is reinstalling all of the packages that tidyquant depends on.

If that doesn't work, try recreating the problem on another computer. If you cannot, I'm going to have to conclude that this is an issue with your machine, and not with tidyquant. Sorry about this.

verajosemanuel commented 7 years ago

sorry but reinstalling didn't work. The only way of making code work was removing library load and using tidyquant:: on every function call, I Imagine this avoids somehow the call to as.POSIXlt

Thanks

pjcrosbie commented 7 years ago

FWIW, I have the same problem. Windows x64 machine, rendering .Rmd to html:

example: a simple .Rmd file:

---
title: "R Notebook"
output:
  html_document:
    df_print: paged
---

```{r}
library(tidyquant)
packageVersion("tidyquant")

produces this:

output file: tidyquant-fail.knit.md

"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS tidyquant-fail.utf8.md --to html --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output tidyquant-fail.html --smart --email-obfuscation none --self-contained --standalone --section-divs --template "C:\Users\pjc\Documents\R\win-library\3.4\rmarkdown\rmd\h\default.html" --no-highlight --variable highlightjs=1 --variable "theme:bootstrap" --include-in-header "d:\tmp\Rtmp4et7mu\rmarkdown-str40643d2c664b.html" --mathjax --variable "mathjax-url:https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML" 

Output created: tidyquant-fail.html
Error in as.POSIXlt(x, tz = tz(x)) : 
  argument "x" is missing, with no default
Calls: .Last -> cat -> date -> date.default -> as.POSIXlt
Execution halted
``
output file: tidyquant-fail.knit.md
packageVersion("tidyquant")
[1] ‘0.5.3’
DavisVaughan commented 7 years ago

@pjc42

Would you believe me if I had a guess that it was the rmarkdown package?

Humor me, try and install my fork of rmarkdown where I have specified base::date().

devtools::install_github("DavisVaughan/rmarkdown")

Then give R a restart just to be sure. Then try and knit the document again.

pjcrosbie commented 7 years ago

Davis, hi. Thanks for the quick response. Happy to help if I can.

I tried your version of rmarkdown but the error persisted.

I does appear that the issue is lubridate though. I get the same error message just using library(lubridate) no tidyquant involved at all.

If I had to guess, and that is all I'm doing because I'm not qualified, is that the RStudio R process created by the "Knit to HTML" is creating a .Last function for the session that includes the problem call to date(). I am not sure how to check this so purely a guess from the console output of the knitr process.

Consider following couple of examples:

Example: 1

In[1]

---
title: "R Notebook"
output: html_document
---

```{r}
library(rmarkdown)

Out[1]


Sat Sep 23 21:44:13 2017
source .Rprofile: C:/Users/pjc/Documents/.Rprofile

  |......................                                           |  33%
  ordinary text without R code

processing file: junk5.Rmd
  |...........................................                      |  67%
label: unnamed-chunk-1
  |.................................................................| 100%
  ordinary text without R code

output file: junk5.knit.md

"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS junk5.utf8.md --to html --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output junk5.html --smart --email-obfuscation none --self-contained --standalone --section-divs --template "C:\Users\pjc\Documents\R\win-library\3.4\rmarkdown\rmd\h\default.html" --no-highlight --variable highlightjs=1 --variable "theme:bootstrap" --include-in-header "d:\tmp\Rtmpu2XT56\rmarkdown-str12d43f952e5c.html" --mathjax --variable "mathjax-url:https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML" 

done:   Sat Sep 23 21:44:14 2017 

Output created: junk5.html

Example 2

this example fails on what looks like the call that should have produced something like: done: Sat Sep 23 21:44:14 2017

Maybe this Calls: .Last -> cat -> date -> date.default -> as.POSIXlt means it is the R process .Last function that has the problem call?

`` In[2]

---
title: "R Notebook"
output: html_document
---

```{r}
library(lubridate)

Out[2]


Sat Sep 23 21:47:59 2017
source .Rprofile: C:/Users/pjc/Documents/.Rprofile

  |......................                                           |  33%
  ordinary text without R code

  |...........................................                      |  67%
label: unnamed-chunk-1

processing file: junk5.Rmd
  |.................................................................| 100%
  ordinary text without R code

"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS junk5.utf8.md --to html --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output junk5.html --smart --email-obfuscation none --self-contained --standalone --section-divs --template "C:\Users\pjc\Documents\R\win-library\3.4\rmarkdown\rmd\h\default.html" --no-highlight --variable highlightjs=1 --variable "theme:bootstrap" --include-in-header "d:\tmp\RtmpcvK0hW\rmarkdown-str8078564b526c.html" --mathjax --variable "mathjax-url:https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML" 
output file: junk5.knit.md

Output created: junk5.html
Error in as.POSIXlt(x, tz = tz(x)) : 
  argument "x" is missing, with no default
Calls: .Last -> cat -> date -> date.default -> as.POSIXlt
Execution halted

for the record, the rmarkdown version

> packageDescription("rmarkdown")
Package: rmarkdown
Type: Package
Title: Dynamic Documents for R
Version: 1.6.0.9004
Authors@R: c( person("JJ", "Allaire", role = c("aut", "cre"), email = "jj@rstudio.com"), person("Joe", "Cheng", role = "aut", email = "joe@rstudio.com"), person("Yihui", "Xie", role = "aut", email =
                        "yihui@rstudio.com"), person("Jonathan", "McPherson", role = "aut", email = "jonathan@rstudio.com"), person("Winston", "Chang", role = "aut", email = "winston@rstudio.com"), person("Jeff",
                        "Allen", role = "aut", email = "jeff@rstudio.com"), person("Hadley", "Wickham", role = "aut", email = "hadley@rstudio.com"), person("Aron", "Atkins", role = "aut", email = "aron@rstudio.com"),
                        person("Rob", "Hyndman", role = "aut", email = "Rob.Hyndman@monash.edu"), person("Ruben", "Arslan", role = "aut", email = "ruben.arslan@uni-goettingen.de"), person(family = "RStudio, Inc.", role
                        = "cph"), person(family = "jQuery Foundation", role = "cph", comment = "jQuery library"), person(family = "jQuery contributors", role = c("ctb", "cph"), comment = "jQuery library; authors listed
                        in inst/rmd/h/jquery-AUTHORS.txt"), person(family = "jQuery UI contributors", role = c("ctb", "cph"), comment = "jQuery UI library; authors listed in inst/rmd/h/jqueryui-AUTHORS.txt"),
                        person("Mark", "Otto", role = "ctb", comment = "Bootstrap library"), person("Jacob", "Thornton", role = "ctb", comment = "Bootstrap library"), person(family = "Bootstrap contributors", role =
                        "ctb", comment = "Bootstrap library"), person(family = "Twitter, Inc", role = "cph", comment = "Bootstrap library"), person("Alexander", "Farkas", role = c("ctb", "cph"), comment = "html5shiv
                        library"), person("Scott", "Jehl", role = c("ctb", "cph"), comment = "Respond.js library"), person("Ivan", "Sagalaev", role = c("ctb", "cph"), comment = "highlight.js library"), person("Greg",
                        "Franko", role = c("ctb", "cph"), comment = "tocify library"), person("Eli", "Grey", role = c("ctb", "cph"), comment = "FileSaver library"), person("John", "MacFarlane", role = c("ctb", "cph"),
                        comment = "Pandoc templates"), person(family = "Google, Inc.", role = c("ctb", "cph"), comment = "ioslides library"), person("Dave", "Raggett", role = "ctb", comment = "slidy library"),
                        person(family = "W3C", role = "cph", comment = "slidy library"), person("Dave", "Gandy", role = c("ctb", "cph"), comment = "Font-Awesome"), person("Ben", "Sperry", role = "ctb", comment =
                        "Ionicons"), person(family = "Drifty", role = "cph", comment = "Ionicons"), person("Aidan", "Lister", role = c("ctb", "cph"), comment = "jQuery StickyTabs") )
Author: JJ Allaire, Jonathan McPherson, Yihui Xie, Hadley Wickham, Joe Cheng, Jeff Allen
Maintainer: JJ Allaire <jj@rstudio.com>
Description: Convert R Markdown documents into a variety of formats.
Depends: R (>= 3.0)
Imports: tools, utils, knitr (>= 1.14), yaml (>= 2.1.5), htmltools (>= 0.3.5), caTools, evaluate (>= 0.8), base64enc, jsonlite, rprojroot, methods, stringr (>= 1.2.0)
Suggests: shiny (>= 0.11), tufte, testthat, digest, tibble
SystemRequirements: pandoc (>= 1.12.3) - http://pandoc.org
URL: http://rmarkdown.rstudio.com
License: GPL-3
RoxygenNote: 6.0.1
Built: R 3.4.1; ; 2017-09-24 04:28:04 UTC; windows
RemoteType: github
RemoteHost: https://api.github.com
RemoteRepo: rmarkdown
RemoteUsername: DavisVaughan
RemoteRef: master
RemoteSha: f85ba35a099edf7ade89cea95817826d2d1492e5
GithubRepo: rmarkdown
GithubUsername: DavisVaughan
GithubRef: master
GithubSHA1: f85ba35a099edf7ade89cea95817826d2d1492e5

-- File: C:/Users/pjc/Documents/R/win-library/3.4/rmarkdown/Meta/package.rds 
DavisVaughan commented 7 years ago

@pjc42, glad to know we've at least tracked this down to lubridate + something else.

I'd like to figure out exactly what else is the problem. I want to figure out which package that date() call is coming from.

Can you run the following:

rmarkdown::render("path_to_file.Rmd")

And when that fails, try running:

traceback()

Interestingly, on my Mac and my version of RStudio and all the packages, I don't get that done: Sat Sep 23 21:44:14 2017 output even if it works. This is likely why i dont have the problem and you do.

pjcrosbie commented 7 years ago

@DavisVaughan, good morning.

Rendering manually from a script works. What's different? - from the console output below you can see there are no bracketing calls with start and end time-date of the rendering and as you note these are the calls causing the issue.

Again, I'm guessing but I think that this makes it even more clear that the issue is coming from the call in RStudio from Knit to HTML menu item that creates a new R process to run the rmarkdown::render("path_to_file.Rmd") call.

RStudio itself must be adding something equivalent to:

.First <- function() {... start date/info ...}
.Last <- function()  {... end data/time info cat() with problem date() call...}

I am using RStudio: Version 1.1.364


anyway, to facts, here is what I get when I run the render directly:


> rmarkdown::render("eg-lubridate-knitr-issue.Rmd")

processing file: eg-lubridate-knitr-issue.Rmd
  |......................                                           |  33%
  ordinary text without R code

  |...........................................                      |  67%
label: unnamed-chunk-1
  |.................................................................| 100%
  ordinary text without R code

output file: eg-lubridate-knitr-issue.knit.md

"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS eg-lubridate-knitr-issue.utf8.md --to html --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output eg-lubridate-knitr-issue.html --smart --email-obfuscation none --self-contained --standalone --section-divs --template "C:\Users\pjc\Documents\R\win-library\3.4\rmarkdown\rmd\h\default.html" --no-highlight --variable highlightjs=1 --variable "theme:bootstrap" --include-in-header "d:\tmp\RtmpWcoZi7\rmarkdown-str6b6c770e5a85.html" --mathjax --variable "mathjax-url:https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML" 

Output created: eg-lubridate-knitr-issue.html

I posted a question on RStudio community see here: RStudio - knit to html / lubridate issue.

DavisVaughan commented 7 years ago

I have another guess! Do you have an .Rprofile somewhere?? It runs at startup and runs .Last() right before shutting down. The example there even shows using .Last which calls cat which calls date().

http://www.statmethods.net/interface/customizing.html

Even more, an issue on lubridate reported this exact problem.

https://github.com/tidyverse/lubridate/issues/570

Check your home directory, and on a Mac I can do file.edit(~/.Rprofile) to open mine up.

Edit) Based on your post it should be here C:/Users/pjc/Documents/.Rprofile. I am convinced that you DO have an .Rprofile somewhere and I bet that is the issue.

pjcrosbie commented 7 years ago

how embarrassing, spot on.

fixed by removing a helpful date() in a .Last function I had defined there.

note even sure why I added the helpful date time call or how long it has been there. probably was pasted from example you link to.

Anyway, thank you for your help and deepest apologies for literally wasting your time...

DavisVaughan commented 7 years ago

Glad you got it worked out! Really wish it told you which file the error came from. Would have made life a lot easier.

@verajosemanuel you should check for this too.

verajosemanuel commented 7 years ago

Yeah! Confirmed. I've got a date() call in .Last function Solved!

Thanks