1

am wondering why this issue is happening and if there is a fix for it. I create the following sql query below:

 CompanyNames<-sqlQuery(connection, 
                  paste("SELECT Companynm",
                        " FROM RiskMgm_Company",
                        " WHERE CompanyID = ParentID",
                        " ORDER BY Companynm"))

CompanyNames comes in the form of a database. Now no matter how I try or which way I convert it the selectInput below provides me with a list from 1 to 118 rather than the company names... Any idea why and how to fix?

output$CompNameSelector <- renderUI({
selectInput("compName","Company Name:",c(CompanyNames$Companynm))
})

Thanks!

The SQL query returns a data.frame object with 1 variable and 118 observations. All these observations are simply text. Performing
class(CompanyNames$Companynm)
provides a factor type

M--
  • 25,431
  • 8
  • 61
  • 93
James Willcox
  • 631
  • 1
  • 10
  • 15

1 Answers1

4

Since your example isn't reproducible, I'm going to guess that class(CompanyNames$Companynm) is a "factor" rather than a "character". When you use c() on a factor, it strips all the information used for the levels and converts it to a simple numeric column. For example

x<-data.frame(a=c("apple","banana","pear"))
x$a 
# [1] apple  banana pear  
# Levels: apple banana pear
c(x$a)
# [1] 1 2 3

so don't use c() unnecessarily. That should just be used for concatenating vectors. To be safe, convert the factor to a character value with

output$CompNameSelector <- renderUI({
    selectInput("compName","Company Name:",
        as.character(CompanyNames$Companynm))
})
Community
  • 1
  • 1
MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • Thanks for your reply, unfortunately that doesn't work either. Yes when i use class(CompanyNames$Companynm) I get "factor". However using just CompanyNames$Companynm still just gives me a list of numbers :( – James Willcox Jan 29 '15 at 05:03
  • That worked a charm thank you! Sorry I couldn't provide much more than I gave – James Willcox Jan 29 '15 at 05:08