Need help converting data.frame into a vector of mode list

#1
Good day,

I'm fairly new to R and still trying to get a grasp op its data-types as well as conversion between them.

I need to convert a data frame generated by the RMySql package into a list of objects, so that I can use it in a shiny renderUI definition.

I.e. given I have the following code retrieving data from a mysql database:

Code:
 28   getQuestionsForCenter <- function(centerid){
 29     sqlQuery <- paste("SELECT `idquestions`, `text`, `order`, `type` FROM questions WHERE ficentre = ", centerid)
 30     sqlData <- dbGetQuery(con, sqlQuery)
 31     #dput(margin.table(sqlData,c(1,'text')))
 32     #questions.list <- as.list(as.data.frame(t(sqlData)))
 33     #questions.list <- split(sqlData,rownames(sqlData))
 34     #questions.list <- split(sqlData,colnames(sqlData))
 35     #questions.list <- sqlData[c("idquestions","text")]
 36     #questions.list <- lapply(sqlData, parseSql)
 37     return(questions.list)
 38   }

And the following renderUI function:

Code:
 59   output$questionSelect <- renderUI({
 60     questions <- getQuestionsForCenter(input$center)
 61     selectInput("question", "Choose a question:", questions)
 62   })

Then how can transform the data frame returned by the above query into a list suitable for use in the selectInput() function?

Knowing that the result of the query will be something along these lines:

Code:
+-------------+-------------------------------------------------------+-------+------+
| idquestions | text                                                  | order | type |
+-------------+-------------------------------------------------------+-------+------+
|          12 | Ech hunn d'Erklärungen vum Animateur gutt verstanen.  |     1 | q    |
|          13 | Den Animateur ass op d'Froe vun der Klass agaangen.   |     2 | q    |
|          14 | Den Animateur huet no mir gekuckt                     |     3 | q    |
|          15 | Ech hunn de Stage interessant fonnt.                  |     4 | q    |
|          16 | Ech hu mech wuel gefillt beim Schaffen.               |     5 | q    |
+-------------+-------------------------------------------------------+-------+------+

How could I convert this into a list that would have the text column as key and the idquestions as value?

Code:
$`Ech hu mech wuel gefillt beim Schaffen.`
[1] 16

$`Ech hunn de Stage interessant fonnt.`
[1] 15

You can see that I've been dabbling with several functions but haven't found a way to create the required key, value pairs.

Thanks and best regards,
David
 

trinker

ggplot2orBust
#2
You have done some nice things in setting up this questions and some things that are less likely to get you a response.

First off you use code tags which is awesome. Rarely do first time posters use them. +1

  1. The beginning 2/3 of the post is specific to you and would require a bit of parsing to understand and it really isn't part of the problem you're trying to overcome.
  2. You leave row numbers in your code. THat means people helping you have to remove this. Make it easy on others trying to help you.
  3. You give an ascii type representation of your data. This can't be run without some work on the part of people helping you.

I'd suggest you reformat the question to get a better/quicker response.

  1. Provide only what is needed to understand the problem. That means give the data at the point you got stuck (right now you have it as ascii table) and the desired outcome with a brief but sufficient description of the problem.
  2. Use dput to provide your data and/or provide a data set wrapped with code tags

DPUT:
Code:
dat <- structure(list(idquestions = c(12, 13, 14, 15, 16), text = structure(c(4L, 
    1L, 2L, 5L, 3L), .Label = c("Den Animateur ass op d'Froe vun der Klass agaangen.", 
    "Den Animateur huet no mir gekuckt", "Ech hu mech wuel gefillt beim Schaffen.", 
    "Ech hunn d'Erklärungen vum Animateur gutt verstanen.", "Ech hunn de Stage interessant fonnt."
    ), class = "factor"), order = c(1, 2, 3, 4, 5), type = structure(c(1L, 
    1L, 1L, 1L, 1L), .Label = "q", class = "factor")), .Names = c("idquestions", 
    "text", "order", "type"), row.names = c(NA, -5L), class = "data.frame")

CODE WRAPPED DATA:
Code:
  idquestions                                                 text order type
1          12 Ech hunn d'Erklärungen vum Animateur gutt verstanen.     1    q
2          13  Den Animateur ass op d'Froe vun der Klass agaangen.     2    q
3          14                    Den Animateur huet no mir gekuckt     3    q
4          15                 Ech hunn de Stage interessant fonnt.     4    q
5          16              Ech hu mech wuel gefillt beim Schaffen.     5    q
 

trinker

ggplot2orBust
#3
To answer the question (took me about 30 seconds to solve the problem and 5 minutes to get the data in shape)

Code:
  idquestions                                                 text order type
1          12 Ech hunn d'Erklärungen vum Animateur gutt verstanen.     1    q
2          13  Den Animateur ass op d'Froe vun der Klass agaangen.     2    q
3          14                    Den Animateur huet no mir gekuckt     3    q
4          15                 Ech hunn de Stage interessant fonnt.     4    q
5          16              Ech hu mech wuel gefillt beim Schaffen.     5    q

[COLOR="gray"]dat <- structure(list(idquestions = c(12, 13, 14, 15, 16), text = structure(c(4L, 
    1L, 2L, 5L, 3L), .Label = c("Den Animateur ass op d'Froe vun der Klass agaangen.", 
    "Den Animateur huet no mir gekuckt", "Ech hu mech wuel gefillt beim Schaffen.", 
    "Ech hunn d'Erklärungen vum Animateur gutt verstanen.", "Ech hunn de Stage interessant fonnt."
    ), class = "factor"), order = c(1, 2, 3, 4, 5), type = structure(c(1L, 
    1L, 1L, 1L, 1L), .Label = "q", class = "factor")), .Names = c("idquestions", 
    "text", "order", "type"), row.names = c(NA, -5L), class = "data.frame")[/COLOR]

key <- as.list(dat[, 1])
names(key) <- dat[, 2]

[COLOR="gray"]## $`Ech hunn d'Erklärungen vum Animateur gutt verstanen.`
## [1] 12
## 
## $`Den Animateur ass op d'Froe vun der Klass agaangen.`
## [1] 13
## 
## $`Den Animateur huet no mir gekuckt`
## [1] 14
## 
## $`Ech hunn de Stage interessant fonnt.`
## [1] 15
## 
## $`Ech hu mech wuel gefillt beim Schaffen.`
## [1] 16[/COLOR]
 
#4
You have done some nice things in setting up this questions and some things that are less likely to get you a response.
Thanks for your answer and of course also for your guidance. I'll be sure to stick to it for any further posts. This worked nicely once I figured out I had to pass key and not names as an argument to selectInput() ;)
 
Last edited: