R-合并变量和字符串

R-合并变量和字符串,r,function,parsing,R,Function,Parsing,我创建了R函数,用于从数据库加载数据。该函数的一部分如下所示 cars <- function(sk) { get_data <- tbl(ff_ba, "sk_cars") %>% filter(id %in% sk_cars_ids$id) %>% collect() return(get_data) } 函数中的代码应该是 get_data <- tbl(ff_ba, "us_cars") %>%

我创建了R函数,用于从数据库加载数据。该函数的一部分如下所示

cars <- function(sk) {
    get_data <- tbl(ff_ba, "sk_cars") %>%
      filter(id %in% sk_cars_ids$id) %>%
      collect()
  return(get_data)
}
函数中的代码应该是

get_data <- tbl(ff_ba, "us_cars") %>%
          filter(id %in% us_cars_ids$id) %>%
          collect()
      return(get_data)

如果没有访问您的数据库,我无法确认这是否有效,但我认为您可以使用deparsesubstitute并开始做您想做的事情。下面是一个使用mtcars数据集的愚蠢示例

首先,使用一些过滤数据帧来代替x_cars_id数据帧:

mpg_values <- data.frame(values = c(27.3, 30.4, 33.9))
disp_values <- data.frame(values = 100:300)
希望这能为你指明正确的方向

mpg_values <- data.frame(values = c(27.3, 30.4, 33.9))
disp_values <- data.frame(values = 100:300)
custom_filter <- function(prefix) {
  # Convert the input to a string value
  input <- deparse(substitute(prefix))

  # Use get to call the correct filtering df
  filter_df <- get(paste(input, "_values", sep = ""))

  # Subset using the string value
  out <- mtcars[mtcars[input] > 25, ] %>%
    # filter using the df called by 'get', treating
    # the input as our desired filtering variable
    # just for this example
    filter(get(input) %in% filter_df$values)
  return(out)
}
> custom_filter(mpg)
   mpg cyl disp  hp drat    wt  qsec vs am gear carb
1 30.4   4 75.7  52 4.93 1.615 18.52  1  1    4    2
2 33.9   4 71.1  65 4.22 1.835 19.90  1  1    4    1
3 27.3   4 79.0  66 4.08 1.935 18.90  1  1    4    1
4 30.4   4 95.1 113 3.77 1.513 16.90  1  1    5    2


> custom_filter(disp)
   mpg cyl disp  hp drat    wt  qsec vs am gear carb
1 21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
2 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
3 22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
4 21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
5 18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
6 19.7   6  145 175 3.62 2.770 15.50  0  1    5    6
7 21.4   4  121 109 4.11 2.780 18.60  1  1    4    2