Export from R to FormR Survey

85 views
Skip to first unread message

Linus Oberhoff

unread,
Oct 20, 2023, 3:55:03 AM10/20/23
to formr
Hello there,

I want to export a data frame I created in R to a FormR Survey without having to navigate through the FormR Interface.

In my R-Script, the data frame could look something like this:

survey <- data.frame(type = character(),
                               name = character(),
                               label = character(),
                               optional = character(),
                               showif = character())

survey[1, ] <- data.frame("note", "welcome", "Welcome to this FormR-Survey!", "", "")
survey[2, ] <- data.frame("note", "instructions", "Please read the following texts carefully.", "", "")

where survey (empty columns not displayed for simplicity) returns


type name label note welcome Welcome to this FormR-Survey! note instructions Please read the following texts carefully.

Is there a way to directly export this dataframe to FormR and create a survey from it? Any help is greatly appreciated!

Linus Oberhoff

Ruben Arslan

unread,
Oct 20, 2023, 4:14:30 AM10/20/23
to Linus Oberhoff, formr
Hi Linus,

you'd have to generate a csv file or similar and use R's httr package to upload the survey.
See the https://github.com/rubenarslan/formr R package for some code that might help you
see how it can be done.

Please share your solution if you get it working.

Best, 

Ruben

--
You received this message because you are subscribed to the Google Groups "formr" group.
To unsubscribe from this group and stop receiving emails from it, send an email to formr+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/formr/88590ae7-1fd0-4cd4-9046-1be390e47760n%40googlegroups.com.

John Helveston

unread,
Oct 20, 2023, 6:28:27 AM10/20/23
to formr
A slightly different version of this that I have been thinking about is using Quarto to define the content in a survey, and then having it render to an Excel sheet. This would require some custom functions  (and perhaps a custom Quarto extension) where the resulting spreadsheet could simply be uploaded to formr. I don't think it'd be all that difficult to get a basic working version. The challenge I see though is defining the questions. I'm not sure how to do that and have Quarto understand what should go where in the spreadsheet for defining things like options to a multiple choice question. And  of course there's the complexity of inserting code chunks. Quarto will try to run all the code chunks when rendering, so you'd have to define a new code chunk option, maybe something like "embed=TRUE" if you wanted it to actually not run the code chunk but instead insert it into the spreadsheet. Anyway, it's just an idea. I'd love to be able to define my entire survey as a Quarto document that I could render and preview from RStudio before uploading it to formr. It's actually the workflow I typically use anyway - I usually write out a full Quarto doc, render it to html, then if everything looks good I copy-paste the pieces of the qmd file into a Google sheet for formr.

Cheers,
John


Linus Oberhoff

unread,
Oct 30, 2023, 8:21:15 AM10/30/23
to formr
Dear Ruben,

thanks for your response. I was not able to get it working so far.
I'm creating a csv file using

write.csv2(survey, "survey.csv", row.names = F, fileEncoding = "UTF-8")

Manually importing this into FormR works just fine. Trying to upload it with httr:

response <- POST(
url = "url/survey/upload_items",
config = list(authenticate(user = "***", password = "***")),
body = upload_file("directory/survey.csv"),
encode = "json",
handle = NULL
)

response$status_code returns 200, indicating a succesful upload, but the upload is indeed not sucessful, and response returns:

[..] You need to login to access the admin section [...]

which makes me think the authentification is not working. I could not find any functions helping with this in the FormR package.

Best, Linus

Ruben Arslan

unread,
Oct 30, 2023, 8:24:31 AM10/30/23
to formr
Hi Linus,

formr::formr_connect() authenticates you on formr.org. You have to run this successfully first.
You don't need the config line in your call. And the URL should be "https://formr.org/admin/survey/add_survey", not what you had.

Best wishes,

Ruben

Linus Oberhoff

unread,
Oct 30, 2023, 12:32:56 PM10/30/23
to formr
Hi Ruben,

sorry for being unclear. I run formr_connect() before successfully and I also run with the url you specificied, I just wanted to not give away account data.

Best, Linus

Linus Oberhoff

unread,
Nov 2, 2023, 6:41:38 AM11/2/23
to formr

Solved: Ruben was very kind to implement the new function formr_upload_items() in version 0.9.2. of the formr package.

This solution works to upload new surveys:

library(formr)

survey_name <- data.frame(type = character(),


                               name = character(),
                               label = character(),
                               optional = character(),
                               showif = character())

survey_name[1, ] <- data.frame("note", "welcome", "Welcome to this FormR-Survey!", "", "")

survey_name[2, ] <- data.frame("note", "instructions", "Please read the following texts carefully.", "", "")

write.csv2(survey_name, "survey_name.csv", row.names = F, fileEncoding = "UTF-8")

# specify host according to the adress where your formr account is hosted, defaults to https://formr.org
formr_connect(email = "***", password = "***", host = "https://formr.uni-muenster.de")
formr_upload_items(survey_file_path = "survey_name.csv", host = "https://formr.uni-muenster.de/")

Although Ruben does not recommend updating existing surveys because of the sanity checks required to prevent data deletion, this code will work to do it:

survey_name[1, "label"] <- "Updated Welcome Message"
write.csv2(survey_name, "survey_name.csv", row.names = F, fileEncoding = "UTF-8")

httr::POST(
  url = "https://formr.uni-muenster.de/admin/survey/survey_name/upload_items",
  body = list(uploaded = httr::upload_file("survey_name.csv"))
)
Reply all
Reply to author
Forward
0 new messages