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

Tidy code style with use of |> instead of %>% #115

Open
asadow opened this issue Jul 28, 2023 · 1 comment
Open

Tidy code style with use of |> instead of %>% #115

asadow opened this issue Jul 28, 2023 · 1 comment

Comments

@asadow
Copy link

asadow commented Jul 28, 2023

Currently with the use of |>, code does not display in tidy style. A reprex is shown below, where using |> causes the third line of displayed code to be

downloads_rolling <- mutate(downloads, count = zoo::rollapply(count, 7, mean, fill = "extend"))

but using %>% instead will give this:

downloads_rolling <- downloads %>%
  mutate(count = zoo::rollapply(count, 7, mean, fill = "extend"))

Is there any way to use |> instead and get this?

downloads_rolling <- downloads |>
  mutate(count = zoo::rollapply(count, 7, mean, fill = "extend"))

Reprex:

library(shiny)
library(tidyverse)
library(shinymeta)

ui <- fluidPage(
  textInput("package", "Package name", value = "ggplot2"),
  verbatimTextOutput("code"),
  plotOutput("plot")
)

server <- function(input, output, session) {

  downloads <- metaReactive({
    cranlogs::cran_downloads(..(input$package), from = Sys.Date() - 365, to = Sys.Date())
  })

  downloads_rolling <- metaReactive2({
    validate(need(sum(downloads()$count) > 0, "Input a valid package name"))

    metaExpr({
      ..(downloads()) |>
        mutate(count = zoo::rollapply(count, 7, mean, fill = "extend"))
    })
  })

  output$plot <- metaRender(renderPlot, {
    ggplot(..(downloads_rolling()), aes(date, count)) + geom_line()
  })

  output$code <- renderPrint({
    expandChain(
      quote(library(tidyverse)),
      output$plot()
    )
  })
}

shinyApp(ui, server)
@jcheng5
Copy link
Member

jcheng5 commented Apr 24, 2024

Wow, that is a huge bummer. The R parser seems to throw the native pipe away immediately:

> quote(
+   ..(downloads()) |>
+     mutate(count = zoo::rollapply(count, 7, mean, fill = "extend"))
+ )
mutate(..(downloads()), count = zoo::rollapply(count, 7, mean, 
    fill = "extend"))

I can't immediately think of an easy fix for this. Maybe forcing the app author to use a stand-in for the native pipe, like %|>%, that we then clean up to |> during rendering?

Or having you write the code using magrittr pipes but having an option to replace %>% with |> during code generation... but that's a little dangerous because you could end up with code that works at runtime but not if you use the generated code due to differences between the two pipes.

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

2 participants