Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Update Oct 2014</strong>. Now in data.table v1.9.5 :</p> <blockquote> <p><code>:=</code> no longer prints in <code>knitr</code> for consistency with behaviour at the prompt, <a href="https://github.com/Rdatatable/data.table/issues/505" rel="nofollow noreferrer">#505</a>. Output of a test <code>knit("knitr.Rmd")</code> is now in data.table's unit tests.</p> </blockquote> <p>and related :</p> <blockquote> <p><code>if (TRUE) DT[,LHS:=RHS]</code> now doesn't print (thanks to Jureiss, <a href="https://github.com/Rdatatable/data.table/issues/869" rel="nofollow noreferrer">#869</a>). Test added. To get this to work we've had to live with one downside: if a <code>:=</code> is used inside a function with no <code>DT[]</code> before the end of the function, then the next time <code>DT</code> is typed at the prompt, nothing will be printed. A repeated <code>DT</code> will print. To avoid this: include a <code>DT[]</code> after the last <code>:=</code> in your function. If that is not possible (e.g., it's not a function you can change) then <code>print(DT)</code> and <code>DT[]</code> at the prompt are guaranteed to print. As before, adding an extra <code>[]</code> on the end of a <code>:=</code> query is a recommended idiom to update and then print; e.g. <code>&gt; DT[,foo:=3L][]</code></p> </blockquote> <hr> <hr> <p>Previous answer kept for posterity (the <code>global$depthtrigger</code> business is no longer done as from data.table v1.9.5 so this is no longer true) ...</p> <p>Just to be clear I understand then: <code>knitr</code> is printing when you don't want it to.</p> <p>Try increasing <code>data.table:::.global$depthtrigger</code> a little bit at the start of the script.</p> <p>This will be 3 for you currently :</p> <pre><code>data.table:::.global$depthtrigger [1] 3 </code></pre> <p>I don't know how much eval depth <code>knitr</code> adds to the stack. But try changing the trigger to 4 first; i.e.</p> <pre><code>assign("depthtrigger", 4, data.table:::.global) </code></pre> <p>and at the end of the <code>knitr</code> script ensure to set it back to 3. If 4 doesn't work, try 5, then 6. If you get to 10 give up and I'll think again. ;-P</p> <p>Why might this work?</p> <p>See NEWS from v1.8.4 :</p> <blockquote> <p><code>DT[,LHS:=RHS,...]</code> no longer prints <code>DT</code>. This implements #2128 "Try again to get <code>DT[i,j:=value]</code> to return invisibly". Thanks to discussions here :<br> <a href="https://stackoverflow.com/questions/11359553/how-to-suppress-output-when-using-in-r-data-table">how to suppress output when using `:=` in R {data.table}, prior to v1.8.3?</a> <br> <a href="http://r.789695.n4.nabble.com/Avoiding-print-when-using-tp4643076.html" rel="nofollow noreferrer">http://r.789695.n4.nabble.com/Avoiding-print-when-using-tp4643076.html</a> <br> FAQs 2.21 and 2.22 have been updated.</p> <p><em>FAQ 2.21 Why does DT[i,col:=value] return the whole of DT? I expected either no visible value (consistent with &lt;-), or a message or return value containing how many rows were updated. It isn't obvious that the data has indeed been updated by reference.</em> <br> This has changed in v1.8.3 to meet your expectations. Please upgrade. The whole of DT is returned (now invisibly) so that compound syntax can work; e.g., DT[i,done:=TRUE][,sum(done)]. The number of rows updated is returned when verbosity is on, either on a per query basis or globally using options(datatable.verbose=TRUE).</p> <p><em>FAQ 2.22 Ok, thanks. What was so difficult about the result of DT[i,col:=value] being returned invisibly?</em> <br> R internally forces visibility on for [. The value of FunTab's eval column (see src/main/names.c) for [ is 0 meaning force R_Visible on (see R-Internals section 1.6). Therefore, when we tried invisible() or setting R_Visible to 0 directly ourselves, eval in src/main/eval.c would force it on again. To solve this problem, the key was to stop trying to stop the print method running after a :=. Instead, inside := we now (from v1.8.3) set a global flag which the print method uses to know whether to actually print or not.</p> </blockquote> <p>That global flag is <code>data.table:::.global$print</code>. At the top of <code>data.table:::print.data.table</code> you'll see it looking at it. That's because there is no known way to suppress printing from <code>[</code> (as FAQ 2.22 explains).</p> <p>So, inside <code>:=</code> inside <code>[.data.table</code> it looks to see how "deep" this call is :</p> <pre><code>if (Cstack_info()[["eval_depth"]] &lt;= .global$depthtrigger) { suppPrint = function(x) { .global$print=FALSE; x } # Suppress print when returns ok not on error, bug #2376. # Thanks to: https://stackoverflow.com/a/13606880/403310 # All appropriate returns following this point are # wrapped i.e. return(suppPrint(x)). } </code></pre> <p>Essential that's just saying: if <code>DT[,x:=y]</code> is running at the prompt, then I know the REPL is going to call the <code>print</code> method on my result, beyond my control. Ok, so given <code>print</code> method is going to run, I'm going to suppress it inside that <code>print</code> method by setting a flag (since the <code>print</code> method that runs (i.e. <code>print.data.table</code>) is something I can control).</p> <p>In <code>knitr</code>'s case it's simulating the REPL in a clever way. It isn't really a script, iiuc, otherwise <code>DT[,x:=y]</code> wouldn't print anyway for that reason. But because it's simulating REPL via an <code>eval</code> there is an extra level of <code>eval</code> depth for code run from <code>knitr</code>. Or something similar (I don't know <code>knitr</code>).</p> <p>Which is why I'm thinking increasing the <code>depthtrigger</code> might do the trick.</p> <p>Hacky/crufty, I agree. But if it works, and you let me know which value works, I can change <code>data.table</code> to be <code>knitr</code> aware and change the <code>depthtrigger</code> automatically. Or any better solutions are most welcome.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload