Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用if()在dplyr管道链中使用select()_R_Dplyr - Fatal编程技术网

使用if()在dplyr管道链中使用select()

使用if()在dplyr管道链中使用select(),r,dplyr,R,Dplyr,请阅读以下两篇文章: 我使用的是闪亮的input$selector,如果用户选择了一个特定的值,我希望我的数据帧与其他值不同 这是一条链: filtered_funnel <- reactive({ lastmonth_funnel %>% filter(input$channel == "All" | Channel == input$channel) %>% filter(input$promo == "All" | Promo ==

请阅读以下两篇文章:

我使用的是闪亮的input$selector,如果用户选择了一个特定的值,我希望我的数据帧与其他值不同

这是一条链:

  filtered_funnel <-  reactive({
    lastmonth_funnel %>%
      filter(input$channel == "All" | Channel == input$channel) %>%
      filter(input$promo == "All" | Promo == input$promo) %>%

      ## HERE IS WHERE I'M STRUGGLING
      {if(input$promo != "none") select(., c("Channel", "Promo", "ShippingDetails", "Checkout", "Transactions"))} %>%


      gather(Funnel, Sessions, -Channel, -Promo) %>%
      group_by(Channel, Promo, Funnel) %>%
      summarise(Sessions = sum(Sessions))
  })
我收到了这个错误:

警告:错误:所有select输入必须解析为整数列 位置。以下情况不适用: *C频道、促销、发货详情、结帐、交易

我还尝试:

{if(input$promo != "none") select(., c(Channel, Promo, ShippingDetails, Checkout:Transactions))} %>%
这实际上一直运行,直到我在输入中选择“无”,在这种情况下,我得到

错误:is.characterx不正确

我在尝试此操作时遇到了相同的错误:

{ifelse(input$promo != "none", select(., c(Channel, Promo, ShippingDetails, Checkout:Transactions)), .)} %>%
如何在dplyr管道链中嵌套一个select语句,该语句表示if input$promo!=none然后选择Channel、Promo、ShippingDetails、Checkout:来自管道中传递的对象的事务

-这是随机生成的数据的dput-

> dput(lastmonth_funnel)
structure(list(Channel = c("Facebook", "Youtube", "SEM", "Organic", 
"Direct", "Email", "Facebook", "Youtube", "SEM", "Organic", "Direct", 
"Email", "Facebook", "Youtube", "SEM", "Organic", "Direct", "Email", 
"Facebook", "Youtube", "SEM", "Organic", "Direct", "Email", "Facebook", 
"Youtube", "SEM", "Organic", "Direct", "Email"), Promo = c("none", 
"none", "none", "none", "none", "none", "banannas", "banannas", 
"banannas", "banannas", "banannas", "banannas", "carrots", "carrots", 
"carrots", "carrots", "carrots", "carrots", "pears", "pears", 
"pears", "pears", "pears", "pears", "apples", "apples", "apples", 
"apples", "apples", "apples"), Sessions = c(6587, 3015, 6316, 
11219, 8117, 6473, 12464, 14032, 14318, 17535, 16219, 7838, 10685, 
12040, 19907, 13694, 6187, 16784, 21425, 18890, 24891, 16251, 
16977, 25206, 28573, 18704, 29178, 22069, 39687, 53734), AddToCart = c(279, 
4955, 5636, 8991, 15530, 18374, 9431, 5980, 4852, 5412, 4114, 
1782, 370, 3208, 6311, 9760, 7428, 6792, 3500, 5446, 1507, 783, 
2032, 833, 397, 2760, 5784, 9810, 13274, 14470), Registrations = c(194, 
3210, 3573, 6067, 10305, 12653, 6564, 3874, 3076, 3652, 2730, 
1227, 257, 2078, 4001, 6586, 4929, 4677, 2436, 3528, 955, 528, 
1348, 573, 276, 1788, 3667, 6620, 8808, 9964), ShippingDetails = c(134, 
2235, 2593, 4266, 7408, 9244, 4557, 2698, 2232, 2568, 1962, 896, 
178, 1447, 2904, 4631, 3543, 3417, 1691, 2457, 693, 371, 969, 
418, 191, 1245, 2661, 4655, 6332, 7280), Checkout = c(90, 1436, 
1792, 2864, 4672, 5666, 3078, 1734, 1543, 1724, 1237, 549, 120, 
930, 2007, 3109, 2234, 2094, 1142, 1579, 479, 249, 611, 256, 
129, 800, 1839, 3125, 3993, 4462), Transactions = c(59, 937, 
1192, 1819, 2602, 2926, 2039, 1132, 1026, 1095, 689, 283, 79, 
607, 1335, 1975, 1244, 1081, 756, 1031, 318, 158, 340, 132, 85, 
522, 1223, 1985, 2224, 2304)), class = "data.frame", row.names = c(NA, 
-30L), .Names = c("Channel", "Promo", "Sessions", "AddToCart", 
"Registrations", "ShippingDetails", "Checkout", "Transactions"
))

您需要确保{之间的语句返回data.frame,而不管条件如何。因此,您需要一个else

正确与否都适用


还请注意,像这样一个简单的例子确实使问题更容易理解。

是的,谢谢。当我尝试使用ifelse时,我把它放在了。在else部分,我想知道为什么它不起作用。无论如何,这就是诀窍,谢谢
> dput(lastmonth_funnel)
structure(list(Channel = c("Facebook", "Youtube", "SEM", "Organic", 
"Direct", "Email", "Facebook", "Youtube", "SEM", "Organic", "Direct", 
"Email", "Facebook", "Youtube", "SEM", "Organic", "Direct", "Email", 
"Facebook", "Youtube", "SEM", "Organic", "Direct", "Email", "Facebook", 
"Youtube", "SEM", "Organic", "Direct", "Email"), Promo = c("none", 
"none", "none", "none", "none", "none", "banannas", "banannas", 
"banannas", "banannas", "banannas", "banannas", "carrots", "carrots", 
"carrots", "carrots", "carrots", "carrots", "pears", "pears", 
"pears", "pears", "pears", "pears", "apples", "apples", "apples", 
"apples", "apples", "apples"), Sessions = c(6587, 3015, 6316, 
11219, 8117, 6473, 12464, 14032, 14318, 17535, 16219, 7838, 10685, 
12040, 19907, 13694, 6187, 16784, 21425, 18890, 24891, 16251, 
16977, 25206, 28573, 18704, 29178, 22069, 39687, 53734), AddToCart = c(279, 
4955, 5636, 8991, 15530, 18374, 9431, 5980, 4852, 5412, 4114, 
1782, 370, 3208, 6311, 9760, 7428, 6792, 3500, 5446, 1507, 783, 
2032, 833, 397, 2760, 5784, 9810, 13274, 14470), Registrations = c(194, 
3210, 3573, 6067, 10305, 12653, 6564, 3874, 3076, 3652, 2730, 
1227, 257, 2078, 4001, 6586, 4929, 4677, 2436, 3528, 955, 528, 
1348, 573, 276, 1788, 3667, 6620, 8808, 9964), ShippingDetails = c(134, 
2235, 2593, 4266, 7408, 9244, 4557, 2698, 2232, 2568, 1962, 896, 
178, 1447, 2904, 4631, 3543, 3417, 1691, 2457, 693, 371, 969, 
418, 191, 1245, 2661, 4655, 6332, 7280), Checkout = c(90, 1436, 
1792, 2864, 4672, 5666, 3078, 1734, 1543, 1724, 1237, 549, 120, 
930, 2007, 3109, 2234, 2094, 1142, 1579, 479, 249, 611, 256, 
129, 800, 1839, 3125, 3993, 4462), Transactions = c(59, 937, 
1192, 1819, 2602, 2926, 2039, 1132, 1026, 1095, 689, 283, 79, 
607, 1335, 1975, 1244, 1081, 756, 1031, 318, 158, 340, 132, 85, 
522, 1223, 1985, 2224, 2304)), class = "data.frame", row.names = c(NA, 
-30L), .Names = c("Channel", "Promo", "Sessions", "AddToCart", 
"Registrations", "ShippingDetails", "Checkout", "Transactions"
))
cond <- FALSE

mtcars %>% 
  group_by(cyl) %>% 
  { if (cond) filter(., am == 1) else . } %>% 
  summarise(m = mean(wt))