小编Til*_*ill的帖子

如何在ggplot geoms中编写带有整齐求值的函数?

我想编写一个在ggplot中执行美学映射的函数。该函数应该有两个参数:var应该映射到aesthetic。实际上,下面的第一个代码块有效。

但是,我不想在初始ggplot函数中而是在geom_point函数中进行映射。在这里,我收到以下错误消息:

错误::=只能在准引数中使用

1.阻止:工作正常

library(ggplot2)
myfct <- function(aesthetic, var){
  aesthetic <- enquo(aesthetic)
  var <- enquo(var)
  ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, !! (aesthetic) := !!var)) + 
    geom_point()
}
myfct(size, Petal.Width)
Run Code Online (Sandbox Code Playgroud)

2.阻止:抛出错误

library(ggplot2)
myfct <- function(aesthetic, var){
  aesthetic <- enquo(aesthetic)
  var <- enquo(var)
  ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) + 
    geom_point(aes(!! (aesthetic) := !!var))
}
myfct(size, Petal.Width)
Run Code Online (Sandbox Code Playgroud)

如果自变量审美作为字符串传递时,也会发生相同的行为sym

# 1. block
myfct <- function(aesthetic, var){
  aesthetic <- sym(aesthetic)
  ggplot(iris, aes(x = …
Run Code Online (Sandbox Code Playgroud)

r function ggplot2 tidyeval

7
推荐指数
1
解决办法
80
查看次数

在隐藏并再次显示后,如何防止使用renderUI进行的重置?

我的许多闪亮应用程序的一个常见场景是,有一个可能有趣的过滤器变量的大量列表(通常是10到20),但我想避免让用户混淆过多的输入窗口小部件.

因此,我的策略通常如下: 1.用户可以选择过滤变量.2.如果选择了至少一个过滤器变量,则触发renderUI,其中每个选定变量包含一个输入窗口小部件.3.过滤条件应用于数据,并生成一些输出.

问题是第一步中的任何更改(通过添加或删除过滤器变量)都会消除第二步中所有先前做出的选择.这意味着所有输入窗口小部件都会无意中重置为其默认值.这会妨碍顺畅的用户体验.知道怎么改进这个吗?

在这里你可以看到会发生什么:

无意的小部件重置示例

以下是重现此行为的代码:

library("shiny")
library("dplyr")
library("nycflights13")

df <- flights
filtervarsChoices <- c("origin","carrier")
originChoices <- unique(df$origin)
carrierChoices <- unique(df$carrier)


ui <- fluidPage(
  h3("1. Select Filter variables"),
  selectInput("filterVars", "Filter variables", filtervarsChoices, multiple = TRUE),
  uiOutput("filterConditions"),
  h3("Result"),
  tableOutput("average")

)

server <- function(input, output, session) {
  output$filterConditions <- renderUI({
    req(input$filterVars)
    tagList(
      h3("2. Select Filter values"),
      if ("origin" %in% input$filterVars) {
        selectInput("originFilter", "Origin", originChoices, multiple = TRUE)
      },
      if ("carrier" %in% input$filterVars) { …
Run Code Online (Sandbox Code Playgroud)

r shiny

5
推荐指数
1
解决办法
708
查看次数

对多个任意过滤条件使用 tidy eval

我想使用整洁的评估来编写多个、完全灵活的过滤条件。一个相关但不太复杂的问题已在 Stackoverflow Question 中得到解决。以下代码(改编自上述其他问题)正在运行。它将两个过滤条件应用于gapminder数据集,并返回过滤后的数据。

library(tidyverse)
library(gapminder)

my_filter <- function(df, cols, vals){    
  paste_filter <- function(x, y) quo(!!sym(x) %in% {{y}})
  fp <- pmap(list(cols, vals), paste_filter)
  filter(df, !!!fp)
}

cols <- list("country", "year")
vals = list(c("Albania", "France"), c(2002, 2007))
gapminder %>% my_filter(cols, vals) 
Run Code Online (Sandbox Code Playgroud)

问题:到目前为止,该解决方案仅限于一种类型的过滤运算符 ( %in%)。我想扩展这种方法以接受任意类型的运算符(==, %in%, >, ...)。预期的函数my_filter应该处理以下内容:

cols <- list("country", "year")
ops <- list("%in%", ">=")
vals = list(c("Albania", "France"), 2007))
gapminder %>% my_filter(cols, ops, vals)
Run Code Online (Sandbox Code Playgroud)

我脑海中浮现的用例是闪亮的应用程序。使用这样的功能,我们可以更轻松地让用户对数据集的变量设置任意过滤条件。

r shiny tidyverse tidyeval

1
推荐指数
1
解决办法
230
查看次数

标签 统计

r ×3

shiny ×2

tidyeval ×2

function ×1

ggplot2 ×1

tidyverse ×1