north-road / qgis-processing-r

QGIS Processing R Provider Plugin
https://north-road.github.io/qgis-processing-r/
GNU General Public License v3.0
63 stars 14 forks source link

using dplyr style syntax in Layer[[FieldName]] #133

Closed sriramab closed 5 months ago

sriramab commented 5 months ago

Hi For reasons of comfort and legacy code, I use dplyr and the tidyverse style of R coding. I am trying to write a r script inside qgis. And I would like to do some summary using dplyr::group_by, and then dplyr::summarize. I am currently using the following syntax

layer %>%   group_by(layer[[{{Group1}}]]) %>% summarise(sum_f1 = sum(layer[[Field1]], na.rm=T),

where Group1 is the grouping variable and Field1 is the numeric field I would like summarize (sum) for each group. As dplyr uses variable style column names instead of pure string column names, I am finding it difficult to use dplyr like syntax in qgis-processing-r.

If you see for Group1 in my code, I also tried using the embrace{{}} and also tried the standard way of [[ as you see in my summarize . both styles do not work.

Please guide how I can use the dplyr style syntax in QGIS R scripts.

gavg712 commented 5 months ago

Since the Group1 and Field1 objects in the script generate character objects then the special syntax of the .data pseudo object can be used. With the following .data[[var]] structure. Your script should be written as follows:

##Issue133=name
##Layer=vector
##GroupsFromTable=Field Layer
##VariableToSummarise=Field Layer
##Aggregated=Output vector

library(dplyr)

Aggregated <- Layer %>%   
  dplyr::group_by(.data[[GroupsFromTable]]) %>% 
  dplyr::summarise(sum_f1 = sum(.data[[VariableToSummarise]], na.rm=T))
sriramab commented 5 months ago

Thank you. Your solution works.