Q:

Repeat/duplicate specific row of data frame and append

I would like to duplicate a certain row based on information in a data frame. Prefer a tidyverse solution. I'd like to accomplish this without explicitly calling the original data frame in a function.
Here's a toy example.
data.frame(var1 = c("A", "A", "A", "B", "B"), 
           var2 = c(1, 2, 3, 4, 5), 
           val = c(21, 31, 54, 65, 76))
  var1 var2 val
1    A    1  21
2    A    2  31
3    A    3  54
4    B    4  65
5    B    5  76

All the solutions I've found so far require the user to input the desired row index. I'd like to find a way of doing it programmatically. In this case, I would like to duplicate the row where var1 is "A" with the highest value of var2 for "A" and append to the original data frame. The expected output is
  var1 var2 val
1    A    1  21
2    A    2  31
3    A    3  54
4    B    4  65
5    B    5  76
6    A    3  54

A:

You could select the row that you want to duplicate and add it to original dataframe :
library(dplyr)

var1_variable <- 'A'
df %>%
  filter(var1 == var1_variable) %>%
  slice_max(var2, n = 1) %>%
  #For dplyr < 1.0.0
  #slice(which.max(var2)) %>%
  bind_rows(df, .)

#  var1 var2 val
#1    A    1  21
#2    A    2  31
#3    A    3  54
#4    B    4  65
#5    B    5  76
#6    A    3  54

In base R, that can be done as :
df1 <- subset(df, var1 == var1_variable)
rbind(df, df1[which.max(df1$var2), ])

From this post we can save the previous work in a temporary variable and then bind rows so that we don't break the chain and don't bind the original dataframe df.
df %>%
  #Previous list of commands
  {
    {. -> temp} %>%
      filter(var1 == var1_variable) %>%
      slice_max(var2, n = 1) %>%
      bind_rows(temp)
  }

