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)