{"id":45,"date":"2020-12-18T03:42:30","date_gmt":"2020-12-18T03:42:30","guid":{"rendered":"https:\/\/ap.pstek.nl\/pstek_wp\/blog\/?p=27"},"modified":"2022-06-21T03:01:37","modified_gmt":"2022-06-21T03:01:37","slug":"limer-code-examples","status":"publish","type":"post","link":"https:\/\/ap.pstek.nl\/pstek_wp\/2020\/limer-code-examples\/","title":{"rendered":"limer code examples"},"content":{"rendered":"\n

limer<\/a><\/strong> is an R package that enable R users to connect directly to a Lime Survey installation via its API (for details, see earlier post<\/a>), essentially giving you remote control and a possibility of automating certain procedures.<\/p>\n\n\n\n

Because the documentation of limer and the Lime Survey API<\/a> is a bit minimal and therefore quite confusing for a first-time user, I give some simple coding examples below to get you started.<\/p>\n\n\n\n

First, we connect to our Lime Survey instance, which is installed at LIMESURVEY.URL<\/code> and can be accessed with a LIME.USERNAME<\/code> and LIME.PASSWORD<\/code>. Obviously you should replace these with your own installations’ details in the code below. The get_session_key()<\/code> command gets you a unique key through which you can securely access the Lime Survey installation. This is automatically used in all the limer<\/code> calls.<\/p>\n\n\n\n

library(limer)\n\n#LimeSurvey Server Info\noptions(lime_api = 'https:\/\/LIMESURVEY.URL\/index.php\/admin\/remotecontrol')\noptions(lime_username = 'LIME.USERNAME')\noptions(lime_password = 'LIME.PASSWORD')\n\nget_session_key()<\/code><\/pre>\n\n\n\n

The first example is a simple one that uses a built-in function from limer, get_responses<\/code>. This simply allows you to download all the data (or only completed data) from a particular survey, #10001 in this case.<\/p>\n\n\n\n

responses <- get_responses(10001, sCompletionStatus = 'all')<\/code><\/pre>\n\n\n\n

The second example requires greater knowledge of the LimeSurvey API language because the limer package does not have a neat wrapper for these functions. Instead the generic call_limer<\/code> function is used in which calls from the original API can be introduced. The full guide of these API functions is available here<\/a>. <\/p>\n\n\n\n

The example below involves listing all the surveys on the Lime Survey installation (server) and then getting the number of completed responses. Note that the method<\/code> inserted into the call_limer()<\/code> function is the same method that is listed in the API documentation<\/a> and the params<\/code> are the arguments of that respective method. So in this sense, it’s actually quite straight forward<\/p>\n\n\n\n

call_limer(method = \"list_surveys\") #list surveys on server\n\ncall_limer(method = \"get_summary\", #get number of completed responses\n           params = list(iSurveyID = 10001,\n                         sStatname = \"completed_responses\"))<\/code><\/pre>\n\n\n\n

The third and last example showcases some of the more sophisticated automation options. We aim to copy survey 123456<\/code>, setup a participant table with two extra attributes: Institution<\/code> and File<\/code>, add one participant, activate the survey, compose the survey link and then, delete the survey.<\/p>\n\n\n\n

When the initial survey is copied and users are created, details are stored in tmp<\/code> and tmp2<\/code> because we wish to use these outputs and inputs for later functions.<\/p>\n\n\n\n

The fromJSON<\/code> function (which is from the JSONlite<\/code> package) is also used to feed arrays with multiple pieces of data into the call_limer<\/code> function. There might also be other ways to do this, but the below example works.<\/p>\n\n\n\n

tmp <- call_limer(method = \"copy_survey\", #copy a survey\n                  params = list(iSurveyID_org = 123456,\n                                sNewname = 'The Copied Survey'))\n\ncall_limer(method = \"activate_tokens\", #setup participant table\n           params = list(iSurveyID = tmp$newsid,\n                         aAttributeFields = fromJSON('{\"attribute_1\":\"Institution\",\"attribute_2\":\"File\"}')))\n\ntmp2 <- call_limer(method = \"add_participants\", #add participant\n                   params = list(iSurveyID = tmp$newsid,\n                                 aParticipantsData = fromJSON('[{\"email\":\"bond007@secretservice.gov.uk\",\"lastname\":\"Bond\",\"firstname\":\"James\",\"attribute_1\":\"Secret Service\",\"attribute_2\":\"mi5\",\"usesleft\":999999}]'),\n                                 bCreateToken = TRUE))\n\ncall_limer(method = \"activate_survey\", #activate survey\n           params = list(iSurveyID = tmp$newsid))\n\npaste0('https:\/\/LIMESURVEY.URL\/index.php\/', tmp$newsid, '?token=', tmp2$token, '&newtest=Y') #generate survey link\n\ncall_limer(method = \"delete_survey\", #delete the survey\n           params = list(iSurveyID = tmp$newsid))<\/code><\/pre>\n\n\n\n

As is hopefully clear now, the limer<\/code> package offers some powerful options for automating the setting up of surveys as well as importing the data into R.<\/p>\n\n\n\n

Finally, its good practice to close off the session with the following call.<\/p>\n\n\n\n

release_session_key()<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"

limer is an R package that enable R users to connect directly to a Lime Survey installation via its API (for details, see earlier post), essentially giving you remote control and a possibility of automating certain procedures. Because the documentation of limer and the Lime Survey API is a bit minimal and therefore quite confusing […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[85],"tags":[32,33,43,87,54],"class_list":["post-45","post","type-post","status-publish","format-standard","hentry","category-tutorial","tag-lime-survey","tag-limer","tag-r","tag-software","tag-survey"],"_links":{"self":[{"href":"https:\/\/ap.pstek.nl\/pstek_wp\/wp-json\/wp\/v2\/posts\/45"}],"collection":[{"href":"https:\/\/ap.pstek.nl\/pstek_wp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ap.pstek.nl\/pstek_wp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ap.pstek.nl\/pstek_wp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ap.pstek.nl\/pstek_wp\/wp-json\/wp\/v2\/comments?post=45"}],"version-history":[{"count":1,"href":"https:\/\/ap.pstek.nl\/pstek_wp\/wp-json\/wp\/v2\/posts\/45\/revisions"}],"predecessor-version":[{"id":279,"href":"https:\/\/ap.pstek.nl\/pstek_wp\/wp-json\/wp\/v2\/posts\/45\/revisions\/279"}],"wp:attachment":[{"href":"https:\/\/ap.pstek.nl\/pstek_wp\/wp-json\/wp\/v2\/media?parent=45"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ap.pstek.nl\/pstek_wp\/wp-json\/wp\/v2\/categories?post=45"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ap.pstek.nl\/pstek_wp\/wp-json\/wp\/v2\/tags?post=45"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}