[R] sink() not working as expected

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Wed Nov 3 20:52:11 CET 2021


Hello,

You do not assign the pipe output, so put the print statement as the 
last instruction of the pipe. The following works.

# file: rhelp.R
library(dplyr)

mtcars %>%
   select(mpg, cyl, disp, hp, am) %>%
   mutate(
     sampdt = c("automatic", "manual")[am + 1L]
   ) %>%
   print()


Then, I've just tested it,


source("rhelp.R")


Hope this helps,

Rui Barradas

Às 19:21 de 03/11/21, Rich Shepard escreveu:
> On Wed, 3 Nov 2021, Ivan Krylov wrote:
> 
>> instead. When you source() a script, auto-printing is not performed. This
>> is explained in the first paragraph of ?source, but not ?sink. If you 
>> want
>> to source() scripts and rely on their output (including sink()), you'll
>> need to print() results explicitly.
> 
> Ivan,
> 
> I've read ?source and still do not understand where to put either 
> auto-print
> or an explicit print statement. For example,
> cor_disc %>%
>      select(site_nbr,year, mon, day, hr, min, tz, cfs) %>%
>          mutate(
>              sampdt = make_datetime(year, mon, day, hr, min)
>              )
> print(cor_disc)
> 
> throws an error. I also get an error when the print statement follows the
> sampdt assignment.
> 
> I need to understand how to get a sourced file that's modified (as these
> tibbles are by combining data and time columns into a datatime colune) and
> saving the tibble with the new appended column.
> 
> Rich
> 
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list