Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
360 views
in Technique[技术] by (71.8m points)

r - How to make Shiny's input$var consumable for dplyr::summarise()

I have the following Rmarkdown Shiny:

---
title: "My Title"
runtime: shiny
output: 
  flexdashboard::flex_dashboard:
    vertical_layout: scroll
    theme:  bootstrap
    orientation: rows
---

```{r setup, include=FALSE}
library(flexdashboard)
```

Rows {data-height=700}
-----------------------------------------------------------------------

### Mate-pair Mapping Distribution

```{r mate_pair_distribution, echo=FALSE}
library(ggplot2)
library(tidyverse)
sidebarPanel(
  selectInput("col_id", label = "Features",
              choices = c("carat", "depth","price"), selected = "price"),
  selectInput("op_id", label = "Quality:",
              choices = c("All", "Ideal","Premium","Good","Very Good"), selected = "Good"),

  sliderInput("n_breaks", label = "Number of bins:",
               min = 20, max = 50, value = 30, step = 1)
)


#renderText(input$op_id)

mainPanel(
  renderPlot({
    # Prepare for the data
    dat  <- diamonds %>% filter(cut == input$op_id)
    if(input$op_id == "All") {
      dat <- diamonds
    }

    # Plotting 
    ggplot(dat, aes(dat %>% select(.,contains(input$col_id)))) +
    ggtitle(input$op_id, subtitle = input$col_id) +
    geom_histogram(bins = input$n_breaks) +
    scale_x_continuous() +
    xlab(input$col_id) +
    theme_light()

  }, height=400, width=400),
  br(),
  br(),
  renderPrint({
    dat  <- diamonds %>% filter(cut == input$op_id)
    if(input$op_id == "All") {
      dat <- diamonds
    }

   dat %>% 
      select(.,contains(input$col_id)) %>%
      summarise(mean = mean(input$col_id), sd=sd(input$col_id), n=n())
  })
)

```

Which produce this output

enter image description here

As you can see the renderText() show NA in mean and sd values. It's caused by this line

 dat %>% 
          select(.,contains(input$col_id)) %>%
          summarise(mean = mean(input$col_id), sd=sd(input$col_id), n=n())

So how can I make input$col_id consumable for summarise()? What's the right way to do it?


Non-Shiny context the result is:

> diamonds %>% filter(cut=="Good") %>% select(price)  %>% summarise(mean = mean(price), sd=sd(price), n=n())
# A tibble: 1 × 3
      mean      sd     n
     <dbl>   <dbl> <int>
1 3928.864 3681.59  4906
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Using the development version of dplyr (v0.5.0.9002) you could turn your string into a symbol using rlang::sym() and then use the unquote operator (!! or UQ) to refer to the variable in the dplyr verbs.

library(dplyr)

var1 <- "Good" # replace with input$op_id
var2 <- rlang::sym("price") # replace with input$col_id

diamonds %>%
  filter(cut == var1) %>%
  select_at(vars(!!var2)) %>%
  summarise_at(vars(!!var2), funs(mean, sd, n()))

Which gives:

## A tibble: 1 × 3
#      mean      sd     n
#     <dbl>   <dbl> <int>
#1 3928.864 3681.59  4906

Should you have more than one variable, use rlang::syms() with the unquote splice operator (!!! or UQS). For example:

var1 <- "Good" 
var2 <- rlang::syms(c("price", "depth")) 

diamonds %>%
  filter(cut == var1) %>%
  select_at(vars(UQS(var2))) %>%
  summarise_at(vars(UQS(var2)), funs(mean, sd, n()))

Which gives:

## A tibble: 1 × 6
#  price_mean depth_mean price_sd depth_sd price_n depth_n
#       <dbl>      <dbl>    <dbl>    <dbl>   <int>   <int>
#1   3928.864   62.36588  3681.59 2.169374    4906    4906

For more information, have a look at the quasiquotation section of the Programming with dplyr vignette


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...