The spaces between words in column names of a dataframe cause problem in a shiny app

Viewed 196

I would like to display the summary and visualize the model of a regression by giving other variables as inputs every time. While the code works I changed the column names of the iris datasets by creating spaces between them and now I get error beacuse of this Error in parse: <text>:1:3: unexpected symbol

     library(shiny)
        library(shinydashboard)
        library(shinydashboardPlus)
        library(shinyWidgets)
        library(dplyr)
        library(ggiraph)
        library(ggiraphExtra)
        library(plyr)
        shinyApp(
          ui = dashboardPagePlus(
            header = dashboardHeaderPlus(title = "Social Media Metrics", titleWidth = 320
                                         
            ),
            sidebar = dashboardSidebar(width = 320,
                                       
                                       uiOutput("value"),
                                       uiOutput("value2")
                                       
            ),
            body = dashboardBody(
              verbatimTextOutput("plot"),
              ggiraphOutput("plot2"),
              plotOutput("plot3")
            )
            
            
          ),
          server = function(input, output) {
            
            colnames(iris)[1:4]<-c("f f","s s","d d","f f f")
            
            output$value<-renderUI({
              
              pickerInput(
                inputId = "val"
                ,
                label = "DEPENDENT" 
                ,
                choices = colnames(iris)[-5] #all rows of selected column
                ,
                multiple = F, options = list(`actions-box` = TRUE)
                
              )
              
              
            })
            output$value2<-renderUI({
              
              pickerInput(
                inputId = "val2"
                ,
                label = "INDEPENDENT" 
                ,
                choices = colnames(iris)[-5] #all rows of selected column
                ,
                multiple =T, options = list(`actions-box` = TRUE)
                
              )
            })
            
            
            
            model <- eventReactive(c(input$val,input$val2),{
              req(c(input$val,input$val2))
              lm(as.formula(paste(input$val," ~ ",paste(input$val2,collapse="+"))),data=iris)
            })
            
            output$plot <- renderPrint({
              summary(model())
            })
            output$plot2 <- renderggiraph({
              ggPredict(model(),se=TRUE,interactive=TRUE)
            })
            output$plot3<-renderPlot({
ggplot(iris,aes(y=input$val,x=input$val2))+geom_point()+geom_smooth(method="lm")
})

            
            
            
          }
        )
1 Answers

You should use backticks to define the model variables if they contain spaces:

lm(as.formula(paste0("`",input$val,"` ~ ",paste0("`",input$val2,"`",collapse="+"))),data=iris)

This makes the model work.
However, this doesn't work properly with ggpredict because the backticks aren't saved in the model itself: enter image description here

Looking at source of ggpredict shows that it seems difficult to overcome this difficulty without modifying the code of the function, see for example line 15 which causes the error above :

temp = paste0("aes(y=", yname, ",x=", xname)
Related