Error: Cant Subset Columns that dont exist

Hi Friends,

Trying to perform summary stats on data . Tried removing space and other characters from column names but error exists. It mentions grouped column does not exist .
There are multiple grouped columns in actual data, even on single column it does not work
Below is similar example

Please note get an error as below error :
Problem with mutate() input ..1
e[31mxe[39m Can't subset columns that don't exist.
e[31mxe[39m Column Z doesn't exist.

library(shiny)
library(tidyverse)
library(purrr)
library(rlang)
library(janitor)
library(stringr)

x<- rnorm(10,200,10)
y<- rnorm(10,120,25)
Z <- rep(letters[1:2],75)

df1<-data.frame(x,y,Z)


ui<-fluidPage(
  selectInput("Sales", "Select either x or y",choices=names(df1),selected="x"),
  selectInput("Forecast","Select X or Y",choices = names(df1),selected="y"),
  selectInput("Group","Select Variables to Group",choices="Z",selected="Z"),
  tableOutput("table"),
  verbatimTextOutput("print1"),
  verbatimTextOutput("print2"),
  tableOutput("table2"),
  verbatimTextOutput("checkequality"),

)

server<-function(input,output,session) {
  
  nested_obj <-reactive({
    df1 %>%
    group_by(across(all_of(input$Group))) %>%
    nest()
  })
  
  F_MAPE_NEST <- function(base,subdf,xname,yname){
    subdf <- ensym(subdf)
    mutate(base,
           Error=map(!!subdf,
                     function(df) abs(df[[xname]]-df[[yname]])/pmax(df[[xname]],df[[yname]])
           ))
  }   
  
  table1<-reactive({
    
    F1<-F_MAPE_NEST(nested_obj(),data,input$Sales,input$Forecast)%>%
      unnest
    names(F1)<-map(colnames(F1),~trimws(.x,whitespace="[ \n\t\t\v\f]"))
    F1
   
  })
  output$table<-renderTable({
    
    table1()
  })
    
 
  
  
  output$print1<-renderPrint({
    
    colnames(df1)
  })
  
  output$print2<-renderPrint({
    
    colnames(table1())
  })
   
  output$checkequality <- renderPrint ({
    
    Reduce(setdiff, strsplit(c(names(df1),names(table1())), split = " "))
    
  })
  
     

output$table2 <-renderTable({
  table1() %>%
  group_by(across(all_of(input$Group)))%>%
  summarise(mean_sales=mean(table1()$x))
  
  
})


}
shinyApp(ui=ui,server=server)



try this ?

   table1() %>%
      group_by(input$Group)%>%
      summarise(mean_sales=mean(table1()$x))

Hi Sir , Thanks for help and time

Expected output is based on below code in R

x<- rnorm(10,200,10)
y<- rnorm(10,120,25)
Z <- rep(letters[1:2],10)

df1<-data.frame(x,y,Z)

df1 %>%
  group_by(Z)%>%
  summarise(mean_sales = mean(x))

The suggestion mentioned when i try
does not give the result on groups in z (a,b) .
Also when the groups are more than one in the real example throws an error.


yeah, sorry you need tidyeval from rlang.

x<- rnorm(10,200,10)
y<- rnorm(10,120,25)
Z <- rep(letters[1:2],10)

df1<-data.frame(x,y,Z)

df1 %>%
  group_by(!!!syms(c("Z","y")))%>%
  summarise(mean_sales = mean(x))
1 Like

Hi Sir , Thanks for the help
In main file below code worked perfectly had to add the !! and sym for input to summarize

compute_mape_wtvol() %>% group_by(!!!syms(c(input$Group))) %>% 
       summarise(Accuracy =1- sum(!!sym(input$WtErrorVol)))

Little more clear with !! ,!!! and sym and syms, ensym is still not clear.

Appreciate your help and time , would not be possible without your solutions.

Thanks

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.