Home > OS >  R: Creating a pipe
R: Creating a pipe

Time:07-19

library(tidyverse)

I have 3 functions that are performed correctly separately (I do not specify whole functions here because they are quite long)

Function 1:

duplicates(Data, '30 min')
# saves the results of duplicates for use in other functions
ds <- duplicates(Data, '30 min')

Function 2:

funPlot(ds, "plot1.pdf")

Function 3:

funPlot2(ds, "plot2.pdf")

I would like these functions and ds <- duplicates (Data, '30 min ') to run in the same order as above but automatically using pipe %>% or some other tool available in R.

I would like to ask if something like this is possible in R and ask for help (because I'm not that advanced in R so I don't know).

CodePudding user response:

If you only need to save ds for the plot functions, try:

Data %>%
duplicates("30 min") %>%
funPlot("plot1.pdf")

The pipe makes something the first argument in the next function.

I'm not sure how to do two plots in one sequence though. For two plots I would do:

Data %>%
duplicates("30 min") -> ds

funPlot(ds, "plot1.pdf")
funPlot2(ds, "plot2.pdf")

If by automatically, you mean you want to click one button and have them all run, you can do this in a code chunk in R Markdown.

  • Related