0

Edited for clarity:

I'm building an app in shiny to explore data in a scatterplot. The simplified code is below:

library(shiny)
library(ggplot2)
library(DT)
library(tools)
library(dplyr)
library(tidyverse)

#load data
Data <- Electorate_Data

# Define UI for application 
ui <- fluidPage(
titlePanel("Scatterplot"),

br(),

# Sidebar layout with a input and output definitions
sidebarLayout(
# Inputs
sidebarPanel(
  # Select variable for y-axis
  selectInput(inputId = "y", label = "Y-axis:",
              choices = colnames(Data[6:32])
  ),
  # Select variable for x-axis
  selectInput(inputId = "x", label = "X-axis:",
              choices = colnames(Data[6:32])
  ),

  width = 6
),

# Output:
mainPanel(
   # Create a container for tab panels
      tabsetPanel(

    tabPanel(
      title = "Explore the Data",
      # Show scatterplot
      plotOutput(outputId = "scatterplot")
     )

  ),

  width = 6  
)
 )
)

# Define server function required to create the scatterplot
server <- function(input, output) {

  # Create scatterplot object the plotOutput function is expecting
  output$scatterplot <- renderPlot({
    ggplot(data = Data, aes_string(x = input$x, y = input$y)) +
      geom_point() 
  })

}

# Create a Shiny app object
shinyApp(ui = ui, server = server)

My issue is, when I select the column names (using the function colnames) to be the choices in the selectInput function, and the colnames contain spaces (i.e. "Year 12" instead of Year12), my scatterplot doesn't work, giving the error message:

Error: :1:6: unexpected 'in' 1: Born in

Example of error

Code Example

# Select variable for y-axis
  selectInput(inputId = "y", label = "Y-axis:",
              choices = colnames(Data[6:32])

Now, if I alias each variable, for example using the following code, the app works perfectly fine:

# Select variable for y-axis
  selectInput(inputId = "y", label = "Y-axis:",
          choices = "Year12" = "Year 12", 
                    "Born_Aus" = "Born in Australia",
                    "NoSchool" = "% of the Population Above 15 Years of Age that Didn't Attend School")
  ),

My question is - is there a way to alias the names more efficiently in such a way that I can automate the aliasing process. I've tried a few hack approaches using the names() function, but so far it has only thrown up errors.

At the end of the day I can solve this manually, but surely there is a better way.

Thanks

Edit:

I've included a subset of the data if that helps. You would have to change the code of:

  # Select variable for x-axis
  selectInput(inputId = "x", label = "X-axis:",
              choices = colnames(Data[6:32])
  ),

to just

choices = colnames(Data)

for both the X and Y selectInputs

Data:

> head(Electorate_Data[14:18])
  Born in Australia       LOTE    NoSchool    Year12 Median_age
1         0.6126271 0.29805068 0.012132744 0.5481394         36
2         0.6419959 0.27278743 0.006160949 0.4610346         39
3         0.8234175 0.05199925 0.002323880 0.3564276         40
4         0.5633673 0.45200442 0.011578501 0.4933828         38
5         0.8186847 0.06066808 0.005270832 0.2701636         44
6         0.4439803 0.59099798 0.017304021 0.5374834         35
  • 1
    Possible duplicate of [Using ggplot2 with columns that have spaces in their names](https://stackoverflow.com/questions/29133567/using-ggplot2-with-columns-that-have-spaces-in-their-names). The problem is in the `ggplot` function. I would just take the advise of the referred Q&A and make sure you have valid column names to begin with, – Florian Jul 04 '18 at 06:28
  • That is a similar issue, but not entirely applicable to here. Since I am creating an app in Shiny, if I make to column names 'valid' for ggplot, the selectInput list will have names that aren't exactly user friendly for the average person. If I was just creating a plot using ggplot alone, it wouldn't be an issue as I could circumvent the issue using axis labels. –  Jul 05 '18 at 05:05
  • I see. I think the answer by John Paul should solve your issue. Alternatively, you could rename your columns (replace spaces with dots or underscores), and use `gsub` to replace the dots back to spaces for your `selectInput`. – Florian Jul 05 '18 at 08:01

2 Answers2

3

In general, lets assume you have a vector of names that should be returned by the selectInput() called x. And you have a second, equally long vector, of names that you what the user to see when making the selection, called y. You can do what you want by first giving x the names of y like so:

names(x)<-y 

Then you can use x in the choices= argument to selectInput(). The user will see the names from y as the choices, but the selectInput() will return the values from x.

John Paul
  • 12,196
  • 6
  • 55
  • 75
  • This answer was the closest thing to an effiecient solution. Thanks heaps! –  Jul 13 '18 at 00:13
0

I don't know why your shiny does not support the columns names containing spaces. The code below works fine on my machine. Provide a reproducible example is always helpful for others to help you.

library(shiny)
df <- iris[,-5]
colnames(df) <- sub('\\.', ' ', colnames(df))
ui <- fluidPage(
  titlePanel("Scatterplot and Regression Calculator"),

  br(),

  # Sidebar layout with a input and output definitions
  sidebarLayout(
    # Inputs
    sidebarPanel(
      # Select variable for y-axis
      selectInput(inputId = "y", label = "Y-axis:",
                  choices = colnames(df)
      )
    ),
    mainPanel(
      plotOutput(outputId = "plot")
    )
  )
)
server <-function(input, output) {
  output$plot <- renderPlot({
    hist(df[[input$y]])
  })
}
shinyApp(ui = ui, server = server)
VicaYang
  • 544
  • 3
  • 17
  • Sorry, I realised that I actually asked the wrong question. It had been a few weeks since I looked at my issue and I just assumed that the selectInput was the issue. You are correct, it actually does work fine, however it is the scatterplot using ggplot where the issue occurs. I've edited my question to highlight this fact and I've included a simplified version of my code. It would be great if you could look at this question again. Thanks –  Jul 04 '18 at 05:13
  • Can you provide the dataset "Electorate_Data" (or remove some row and columns to make it smaller but still produce the same problem, if the original data is too large.) [Here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) discussed how to use `dput` or something else to paste a data – VicaYang Jul 04 '18 at 05:29
  • I've added a subset of the data in the original question –  Jul 05 '18 at 00:30