Custom grade with dplyr and brilliant

I hope someone can help me with a non-standard estimate when passing variable names to dplyr in a brilliant application. My intention is to be able to select variables to navigate to functions select

and top_n

. I know the function select

has an select_

NSE equivalent , but I'm struggling to work in a brilliant application too.

I have provided an example below which has two lines of comments that I hope to get. The first commented line is for removing the column identified input$var_to_rank

from the output table, and the second commented line (using top_n

) should set the number of top ranked rows to display, and the column that these ranks will be placed on.

library(shiny)
library(dplyr)
data(iris)

shinyApp(
  ui = basicPage(
selectInput("species", "species", choices = levels(iris$Species)),
selectInput("var_to_drop", "Variable to drop", choices = names(iris)[3:4]),
selectInput("var_to_rank", "Variable to rank", choices = names(iris)[1:2]),
numericInput("n.obs", "Top N", 5),

    tableOutput("table")
),

server = function(input, output) {


output$table <- renderTable({

  iris %>% 
    filter(Species == input$species) %>%
  # select_(quote(-input$var_to_drop)) %>%
    top_n(5, Sepal.Length)
  # top_n(n.obs, input$var_to_rank)
  })
}
)

      

Thanks so much for any help and apologies if this question is answered elsewhere.

+3


source to share


1 answer


To solve your first problem: this is how you can achieve what you want with NSE

select_(lazyeval::interp(~ -var, var = as.name(input$var_to_drop)))

      

It might be simpler / shorter, but it works. I know this can be much easier if you want to include rather than remove columns, I cannot define a shorter code that works with-


For your second problem you can achieve the same effect top_n

as like this



cutoff <- iris %>% .[[input$var_to_rank]] %>% sort %>% rev %>% .[input$n.obs]
iris %>% filter_(lazyeval::interp(~ var >= cutoff, var = as.name(input$var_to_rank)))

      


Just for the sake of completeness, I'm leaving the original answer to the second problem:
For your second problem, this is a solution that works slightly differently. I'm not sure if this is what you want. Usage top_n(5)

could potentially return more than 5 rows, so I do similar but guarantee that only 5 rows are returned

iris %>% arrange_(input$var_to_rank) %>% tail(input$n.obs)

      

+3


source







All Articles