cursor postion together with filter() can lead to error when pyping

I have the following code, where the filtering of geo is one line lower then the filter() command itself. If the pype is run with ctrl+enter with cursor anywhere before the filter ends, the code runs properly, but if the cursor is behind filter() (say somewhere on spread()), an error appears:
Error in UseMethod("spread_") : no applicable method for 'spread_' applied to an object of class "function"
I did not expect, that the position where the cursor is set, defines if the pype results in error or not, which appears only if the the first filter argument is lowered by one line. If the code is filter(geo... the cursor position does not matter. Does anybody have a suggestion why such behavior appears?

code:

library(eurostat)
library(dplyr)
library(tidyr)


earn_mw_avgr2 <- get_eurostat("earn_mw_avgr2", stringsAsFactors = FALSE)

df <- earn_mw_avgr2 %>% 
    filter(
        geo %in% c("BE","BG","CZ","DK","DE","EE","IE","EL","ES","FR","HR",
                   "IT","CY","LV","LU","HU","MT","NL","AT","PL","PT","RO",
                   "SI","SK","FI","SE","UK"),
        indic_se=="MW_MEAGE",
        nace_r2=="B-S") %>% 
    spread(time, values)%>%
    select(-c(17,18))%>%
    mutate(avg=rowMeans(.[14:16], na.rm = TRUE))%>%
    arrange(desc(avg))

I believe this is a RSTUDIO IDE issue on how the IDE detect all the pipe statement. When you clik CTRL+ENTER, the IDE try to guess which lines to run. There are some option about that but I don't think there is one about this. You should look anyway.

You may put this thread in the RStudio IDE category maybe

2 Likes

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.