Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First time execute a data.table it doesn't show anything. #2644

Closed
skanskan opened this issue Feb 25, 2018 · 2 comments
Closed

First time execute a data.table it doesn't show anything. #2644

skanskan opened this issue Feb 25, 2018 · 2 comments

Comments

@skanskan
Copy link

skanskan commented Feb 25, 2018

Since the a long time ago all versions of data.table I've been using have a small bug.
Sometimes when I create a data.table using a function with many steps (reading a file and manipulating the result) and I try to show it's contents it doesn't show any result nor errors.

I need to execute its name twice in order to show anything.

It happens in different computers with different R versions and different data.table versions.

Has anybody noticed the same issue?

It's not serious but it could indicate something is wrong in memory.
I'm not able to give a small reproducible example now.

PD:OK, I've got to create a reproducible example.

Create a csv file named prueba.csv with this content.

numb, first,second
1,aaa,2009-12-17
1,bbb,2009-01-28
1,www,2019-12-02
1,ddd,1999-07-01
2,aaa,1999-08-28
2,bbb,2009-05-30
3,bbb,2009-06-14
3,ccc,2009-05-30
3,aaa,1999-05-19
4,ddd,2009-03-24
4,ddd,2009-07-17
5,www,2009-02-02
6,ccc,1999-06-28
6,aaa,1979-07-01
7,bbb,2009-09-09
7,ddd,2009-07-18
8,aaa,2009-03-28
8,ccc,2009-06-08
8,ddd,2009-03-01
9,www,1999-07-17
9,ddd,2009-02-14

Now execute this lines:

lectura <- function() {
     todo <- fread("prueba.csv")  
     todo[,other:=ifelse(any(first=="aaa"),.SD[first=="aaa",first],"2020-10-31"),by=numb]
     return(todo)
}
ttt <- lectura()

Now run
ttt

You will notice it doesn't show its content.
Why?

@franknarf1
Copy link
Contributor

@HughParsonage
Copy link
Member

This is addressed in the FAQ: https://github.com/Rdatatable/data.table/blob/master/vignettes/datatable-faq.Rmd

Why do I have to type DT sometimes twice after using := to print the result to console?

This is an unfortunate downside to get #869 to work. If a := is used inside a function with no DT[] before the end of the function, then the next time DT is typed at the prompt, nothing will be printed. A repeated DT will print. To avoid this: include a DT[] after the last := in your function. If that is not possible (e.g., it's not a function you can change) then print(DT) and DT[] at the prompt are guaranteed to print. As before, adding an extra [] on the end of := query is a recommended idiom to update and then print; e.g.> DT[,foo:=3L][].

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants