如何使 Shiny 的 input$var 可用于 dplyr::summarise() [英] How to make Shiny's input$var consumable for dplyr::summarise()

查看:29
本文介绍了如何使 Shiny 的 input$var 可用于 dplyr::summarise()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下

如您所见,renderText()meansd 值中显示 NA.这是由这条线引起的

 dat %>%select(.,contains(input$col_id))%>%总结(平均值=平均值(输入$col_id),sd=sd(输入$col_id),n=n())

那么我怎样才能让 input$col_id 成为 summarise() 的可消费对象?正确的做法是什么?

<小时>

Non-Shiny 上下文结果是:

>钻石%>%过滤器(切割==好")%>%选择(价格)%>%总结(平均值=平均值(价格),sd=sd(价格),n=n())# 小费:1 × 3平均标准差<dbl><dbl><int>1 3928.864 3681.59 4906

解决方案

使用 dplyr (v0.5.0.9002) 的开发版本,你可以把你的字符串变成使用 rlang::sym() 的符号,然后使用反引号操作符(!!UQ)来引用 dplyr 中的变量动词.

库(dplyr)var1 <- "Good" # 替换为 input$op_idvar2 <- rlang::sym("price") # 替换为 input$col_id钻石 %>%过滤器(剪切 == var1)%>%select_at(vars(!!var2)) %>%summarise_at(vars(!!var2), funs(mean, sd, n()))

给出:

## A tibble: 1 × 3# 平均标准差# <dbl><dbl><int>#1 3928.864 3681.59 4906

<小时>

如果您有多个变量,请使用 rlang::syms() 和不带引号的拼接运算符(!!!UQS).例如:

var1 <- 好"var2 <- rlang::syms(c("price", "depth"))钻石 %>%过滤器(剪切 == var1)%>%select_at(vars(UQS(var2))) %>%summarise_at(vars(UQS(var2)), funs(mean, sd, n()))

给出:

## 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

<小时>

有关更多信息,请查看该指南的准引用部分使用 dplyr 编程 小插图

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

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

解决方案

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

这篇关于如何使 Shiny 的 input$var 可用于 dplyr::summarise()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆