美文网首页
The tidyverse style guide_Part I

The tidyverse style guide_Part I

作者: 城管大队哈队长 | 来源:发表于2020-02-27 23:18 被阅读0次

Chapter 4:Pipes

  • %>% 管道操作应该是用来强调你操作的连贯性而非是你的对象。所以我们应该在两种情况避免使用管道

    • 一次需要操作超过一个对象
    • 一些中间文件是有意义的
  • %>% 前面应该有一个空格,然后后面跟一个新行。然后两个空格符号缩进。

    # Good
    iris %>%
      group_by(Species) %>%
      summarize_if(is.numeric, mean) %>%
      ungroup() %>%
      gather(measure, value, -Species) %>%
      arrange(value)
    
    # Bad
    iris %>% group_by(Species) %>% summarize_all(mean) %>%
    ungroup %>% gather(measure, value, -Species) %>%
    arrange(value)
    
  • 如果函数里面一个参数内容放不下,开一个新行,同时注意缩进。

    iris %>%
      group_by(Species) %>%
      summarise(
        Sepal.Length = mean(Sepal.Length),
        Sepal.Width = mean(Sepal.Width),
        Species = n_distinct(Species)
      )
    
  • 如果是一步的那种管道,就不用开新行了。但这种情况应该考虑重新写成一个正常的函数了……

    # Good
    iris %>% arrange(Species)
    
    iris %>% 
      arrange(Species)
    
    arrange(iris, Species)
    
  • 有时候可以把一个短的管道作为一个长管道中一个函数的参数。但要注意这个操作是否是可读的。如果不是的话,最好还是单独提出来,新建一个变量。

    # Good
    x %>%
      select(a, b, w) %>%
      left_join(y %>% select(a, b, v), by = c("a", "b"))
    
    # Better
    x_join <- x %>% select(a, b, w)
    y_join <- y %>% select(a, b, v)
    left_join(x_join, y_join, by = c("a", "b"))
    
  • magrittr allows you to omit () on functions that don’t have arguments. Avoid this feature.

    # Good
    x %>% 
      unique() %>%
      sort()
    
    # Bad
    x %>% 
      unique %>%
      sort
    
  • 就管道来说,有三种可以接受的赋值方式

    • Variable name and assignment on separate lines:

      iris_long <-
        iris %>%
        gather(measure, value, -Species) %>%
        arrange(-value)
      
    • Variable name and assignment on the same line:

      iris_long <- iris %>%
        gather(measure, value, -Species) %>%
        arrange(-value)
      
    • Assignment at the end of the pipe with ->:

      iris %>%
        gather(measure, value, -Species) %>%
        arrange(-value) ->
        iris_long
      

      hhh,我个人是很喜欢这个骚操作的。但这个骚操作其实是不太易读的……

  • magrittr包还提供了 %<>% 这个操作符帮助你原地修改对象,但应该避免这个操作符

    # Good
    x <- x %>% 
      abs() %>% 
      sort()
      
    # Bad
    x %<>%
      abs() %>% 
      sort()
    

Chapter 5:ggplot2

  • ggplot2的+号的规则类似于 %>%。同时要注意,如果你ggplot2的数据来源于dplyr的管道结果,则只能有一个层次的缩进。

    # Good
    iris %>%
      filter(Species == "setosa") %>%
      ggplot(aes(x = Sepal.Width, y = Sepal.Length)) +
      geom_point()
    
    # Bad
    iris %>%
      filter(Species == "setosa") %>%
      ggplot(aes(x = Sepal.Width, y = Sepal.Length)) +
        geom_point()
    
    # Bad
    iris %>%
      filter(Species == "setosa") %>%
      ggplot(aes(x = Sepal.Width, y = Sepal.Length)) + geom_point()
    
  • 参数太长的话,分开,然后注意缩进

    # Good
    ggplot(aes(x = Sepal.Width, y = Sepal.Length, color = Species)) +
      geom_point() +
      labs(
        x = "Sepal width, in cm",
        y = "Sepal length, in cm",
        title = "Sepal length vs. width of irises"
      ) 
    
    # Bad
    ggplot(aes(x = Sepal.Width, y = Sepal.Length, color = Species)) +
      geom_point() +
      labs(x = "Sepal width, in cm", y = "Sepal length, in cm", title = "Sepal length vs. width of irises") 
    
  • 尽管ggplot2允许你在data参数里面做一些数据操作,但应该尽量避免这种事情,而是应该选择在管道里面完成数据操作。

    # Good
    iris %>%
      filter(Species == "setosa") %>%
      ggplot(aes(x = Sepal.Width, y = Sepal.Length)) +
      geom_point()
    
    # Bad
    ggplot(filter(iris, Species == "setosa"), aes(x = Sepal.Width, y = Sepal.Length)) +
      geom_point()
    

相关文章

网友评论

      本文标题:The tidyverse style guide_Part I

      本文链接:https://www.haomeiwen.com/subject/etqqhhtx.html