2
votes

In my application the user can upload any .csv file and plot it (I have simplified example by using an online file, and I realise the example plot is pretty useless). I am currently having trouble with plotting the stars symbol from the user input.

I want to use a selectizeInput so they can select any number of the columns to plot as the length of the rays. The stars need to take a matrix of the columns but I think selectize returns as a list.

The method I used in the code below shows what I want but it only takes so many columns. I assume there is an easy way to do this but I am quite new to R and could not find anything that worked when I searched.

ui.R

shinyUI(pageWithSidebar(
  headerPanel("Scatterplot of star symbols"),
  sidebarPanel(
    selectizeInput("starSpokes", "Select colums for star spoke lengths", c("murder", "Forcible_rate", "Robbery", "aggravated_assult", "burglary"), multiple = TRUE)
  ),
  mainPanel(
    plotOutput("graphPlot")
  )
))

server.R

shinyServer(function(input, output, session) {
  crime = read.csv("http://datasets.flowingdata.com/crimeRatesByState2005.tsv", header = TRUE, sep ="\t") 

  output$graphPlot = renderPlot({
    starRays = cbind(crime[[input$starSpokes[1]]], crime[[input$starSpokes[2]]], crime[[input$starSpokes[3]]], crime[[input$starSpokes[4]]])
    symbols(x=crime$murder, y=crime$burglary, stars = starRays)
  })
})

I apologise if this has already been answered, I have searched but found nothing that works, possibly because I do not know the correct terms to search for or have implemented their methods wrong.

Any help would be much appreciated, thanks

1

1 Answers

1
votes

Values of the selectizeInput are simply a character vector so you can use it to select columns of the crime data frame. After that all you need is as.matrix.

output$graphPlot <- renderPlot({
    if(length(input$starSpokes) < 3) return()
    symbols(
        x=crime$murder, y=crime$burglary,
        stars=as.matrix(crime[, input$starSpokes])
    )
})

When you work with Shiny it is a good practice to check input and short-circuit if it doesn't meet criteria. Since stars require at least three columns there is no reason to evaluate the rest of the code if user didn't select required number of fields.