# Code component for additional data columns not working in Pavlovia (but does in Builder)

**URL:** <https://discourse.psychopy.org/t/code-component-for-additional-data-columns-not-working-in-pavlovia-but-does-in-builder/31574>\
**Category:** Online experiments\
**Tags:** pavlovia\
**Created:** [October 26, 2022, 11:35am UTC](https://discourse.psychopy.org/t/code-component-for-additional-data-columns-not-working-in-pavlovia-but-does-in-builder/31574 "2022-10-26T11:35:32Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![beckyttyler](https://avatars.discourse-cdn.com/v4/letter/b/edb3f5/32.png) [@beckyttyler](https://discourse.psychopy.org/u/beckyttyler)\
**Post date:** [October 26, 2022, 11:35am UTC](https://discourse.psychopy.org/t/code-component-for-additional-data-columns-not-working-in-pavlovia-but-does-in-builder/31574/1 "2022-10-26T11:35:32Z")

</div>

Hi all

I posted a couple of days ago about a code component for getting spacebar duration (Hold Time) and RT to key press from trial onset (Response Time). Thanks to Jonathan, these both work when run in the builder and the data file adds the variables correctly. See my last post here: [Keyboard press response time from trial onset with coding component](https://discourse.psychopy.org/t/keyboard-press-response-time-from-trial-onset-with-coding-component/31542)

However when uploading to Pavlovia, neither the Hold Time or Response Time (the two additional variables added to the data file) are not pulled through. Hold Time shows a , and response time does not even exist as a column in the data file.

The code I have used is below…experiment also attached if it helps…

**Begin Routine**  
duration =   
rt =   
trialClock = core.Clock()  
kb = keyboard.Keyboard(clock=trialClock)

**Each Frame**  
keys2 = kb.getKeys()  
for key in keys2:  
if key.duration:  
duration.append(key.duration)  
if key.rt:  
rt.append(key.rt)

**End Routine**  
Practice\_Trials\_Loop.addData(“Hold Times”, duration)  
Practice\_Trials\_Loop.addData(“Response Time”, rt)

[Entropy Experiment FIlePractice.psyexp](https://discourse.psychopy.org/uploads/short-url/siPWkfPkTYJ7Myio0yAuwfDEFNd.psyexp) (41.9 KB)

---

<div class="post-metadata">

**Author:** ![ajus](https://avatars.discourse-cdn.com/v4/letter/a/dc4da7/32.png) [@ajus](https://discourse.psychopy.org/u/ajus)\
**Post date:** [October 26, 2022, 11:44am UTC](https://discourse.psychopy.org/t/code-component-for-additional-data-columns-not-working-in-pavlovia-but-does-in-builder/31574/2 "2022-10-26T11:44:29Z")

</div>

Not sure about all the other auto-translated code, but the command to add data to the output in PsychoJS is `psychoJS.experiment.addData('columnname', value)`.

---

<div class="post-metadata">

**Author:** ![beckyttyler](https://avatars.discourse-cdn.com/v4/letter/b/edb3f5/32.png) [@beckyttyler](https://discourse.psychopy.org/u/beckyttyler)\
**Post date:** [October 26, 2022, 1:15pm UTC](https://discourse.psychopy.org/t/code-component-for-additional-data-columns-not-working-in-pavlovia-but-does-in-builder/31574/3 "2022-10-26T13:15:04Z")

</div>

Thank you. I made this amendment (and a couple others which I have mentioned below) and unfortunately its not fully working, however both Hold Time and Response Time are both now showing in the data csv file but both with a instead of the data.

To clarify would it be

psychoJS. **experiment**.addData(“Hold Times”, duration);

OR

psychoJS. **Practice\_Trials\_Loop**.addData(“Hold Times”, duration);

Perhaps the JS code from the code component would help:

**Begin Routine**  
duration = ;  
rt = ;  
trialClock = new util.Clock();  
kb = new core.Keyboard({psychoJS: psychoJS, clock: new util.Clock(), waitForStart: true});

**Each Frame**  
keys2 = kb.getKeys();  
for (var key, \_pj\_c = 0, \_pj\_a = keys2, \_pj\_b = \_pj\_a.length; (\_pj\_c \< \_pj\_b); \_pj\_c += 1) {  
key = \_pj\_a[\_pj\_c];  
if (key.duration) {  
duration.push(key.duration);  
}  
if (key.rt) {  
rt.push(key.rt);  
}  
}

**End Routine**  
psychoJS.experiment.addData(“Hold Times”, duration);  
psychoJS.experiment.addData(“Response Time”, rt)

Also posting the other changes I made - I hope this helps someone else!

I changed **from** trialClock = new core.Clock(); **to** trialClock = new util.Clock(); in Begin Routine in the JS code as pointed out on this post by wakecarter who says this needs to be manually changed as I got a core.Clock error when running in pavlovia - Thanks: [Error: code1Clock = new util.Clock() - #2 by wakecarter](https://discourse.psychopy.org/t/error-code1clock-new-util-clock/20247/2)

I also changed **from** kb = new keyboard.Keyboard({“clock”: trialClock}) **to** kb = new core.Keyboard({psychoJS: psychoJS, clock: new util.Clock(), waitForStart: true}); in the JS code to remove a keyboard undefined error in pavlovia as mentioned in this post here: [Failing to use Keyboard in Pavlovia - #2 by dvbridges](https://discourse.psychopy.org/t/failing-to-use-keyboard-in-pavlovia/13702/2)

---

<div class="post-metadata">

**Author:** ![ajus](https://avatars.discourse-cdn.com/v4/letter/a/dc4da7/32.png) [@ajus](https://discourse.psychopy.org/u/ajus)\
**Post date:** [October 26, 2022, 1:23pm UTC](https://discourse.psychopy.org/t/code-component-for-additional-data-columns-not-working-in-pavlovia-but-does-in-builder/31574/4 "2022-10-26T13:23:55Z")

</div>

Hi @beckyttyler,

> [@beckyttyler](#):
>
> its not fully working

What exactly do you mean here? What does not work? Do you still need help?

---

<div class="post-metadata">

**Author:** ![beckyttyler](https://avatars.discourse-cdn.com/v4/letter/b/edb3f5/32.png) [@beckyttyler](https://discourse.psychopy.org/u/beckyttyler)\
**Post date:** [October 26, 2022, 1:27pm UTC](https://discourse.psychopy.org/t/code-component-for-additional-data-columns-not-working-in-pavlovia-but-does-in-builder/31574/5 "2022-10-26T13:27:55Z")

</div>

Sorry that wasn’t cleat at all.

There is still no data showing in the csv files when downloading from pavlovia.

But with the amendments I made above, both Hold Times and Response Time are showing as columns in the data file, whereas before only Hold Times was. The issue is the missing data.

![Screenshot 2022-10-26 at 14.26.00](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/f/5/f5663ef9d05f5b611f86a546c8b1fc556f85025b.png)

---

<div class="post-metadata">

**Author:** ![ajus](https://avatars.discourse-cdn.com/v4/letter/a/dc4da7/32.png) [@ajus](https://discourse.psychopy.org/u/ajus)\
**Post date:** [October 26, 2022, 1:51pm UTC](https://discourse.psychopy.org/t/code-component-for-additional-data-columns-not-working-in-pavlovia-but-does-in-builder/31574/6 "2022-10-26T13:51:51Z")

</div>

Could you try replacing your each-frame-code with this?

```auto
keys2 = kb.getKeys();
for (var i in keys2) {
  key = keys2[i];
  if (key.duration) {
    duration.push(key.duration);
  }
  if (key.rt) {
    rt.push(key.rt);
  }
}

```

---

<div class="post-metadata">

**Author:** ![beckyttyler](https://avatars.discourse-cdn.com/v4/letter/b/edb3f5/32.png) [@beckyttyler](https://discourse.psychopy.org/u/beckyttyler)\
**Post date:** [October 26, 2022, 1:59pm UTC](https://discourse.psychopy.org/t/code-component-for-additional-data-columns-not-working-in-pavlovia-but-does-in-builder/31574/7 "2022-10-26T13:59:54Z")

</div>

> [@ajus](#):
>
> ```auto
> keys2 = kb.getKeys();
> for (var i in keys2) {
> key = keys2[i];
> if (key.duration) {
> duration.push(key.duration);
> }
> if (key.rt) {
> rt.push(key.rt);
> }
> }
> 
> ```

Thank you for your help and suggestions, but unfortunately there is still no data for Hold Times and Response Time showing in the data file, and instead a

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [October 26, 2022, 2:55pm UTC](https://discourse.psychopy.org/t/code-component-for-additional-data-columns-not-working-in-pavlovia-but-does-in-builder/31574/8 "2022-10-26T14:55:53Z")

</div>

I haven’t successfully measured keypress durations online yet.

Are you expecting simultaneous key presses or different keys?

I’ve previously used mouse button presses when I want press duration online.

> [@Record duration mouse press](https://discourse.psychopy.org/t/record-duration-mouse-press/20327):
>
> Hi guys, I want to record the duration of a mouse press in my experiment. I thought I had found a pretty straightforward solution that worked offline (see code below), but the duration that is recorded for the online version is always somewhere around 0.167 (1 frame I guess?). clock is defined at the beginning of the exp (in js code component): sw = new util.Clock(); Each frame (in py-\> auto code component): if mouse1.isPressedIn(polygonA): sw.reset() sound\_15.play() if sound\_15.status =…

> [@Key Press Duration values are greater than the number of key presses/inaccurate](https://discourse.psychopy.org/t/key-press-duration-values-are-greater-than-the-number-of-key-presses-inaccurate/26749):
>
> URL of experiment: [ToneTask [PsychoPy]](https://run.pavlovia.org/ranafs/tonetask/) Gitlab: [Fareeha Rana / ToneTask · GitLab](https://gitlab.pavlovia.org/ranafs/tonetask) Description of the problem: I’m looking to record the duration of multiple presses of the same key during a given trial. The task premise is for participants to replicate a tone sequence using the spacebar. The experiment is running OK online, but the output file isn’t recording the durations properly–even though it is doing so offline. I used this comment to add code snippets to record keypress durations {[Measuri…](https://discourse.psychopy.org/t/measuring-key-lift-time-relative-to-stimulus-onset/12471/4)

---

<div class="post-metadata">

**Author:** ![beckyttyler](https://avatars.discourse-cdn.com/v4/letter/b/edb3f5/32.png) [@beckyttyler](https://discourse.psychopy.org/u/beckyttyler)\
**Post date:** [October 26, 2022, 3:13pm UTC](https://discourse.psychopy.org/t/code-component-for-additional-data-columns-not-working-in-pavlovia-but-does-in-builder/31574/10 "2022-10-26T15:13:47Z")

</div>

Hi wakecarter

I am measuring just one key press duration RT per trial (using the space bar) as its a time reproduction experiment.

I have managed to successfully get key press duration RT of the space bar (which I have called Hold Times), and I currently have this working on pavlovia, with Hold Times showing in the csv. linked here [Pavlovia](https://pavlovia.org/Beckyttyler/entropy)

However I have needed to make some amendments to the code to also try to get the RT from trial onset to start of the key press to work on pavlovia but without much luck…now neither Hold Times or Response Times are showing in the data file. Note that the experiment I am enquiring about is the same as the linked one above, but I just have one ‘master’ copy (which is [Pavlovia](https://pavlovia.org/Beckyttyler/entropy) linked above), and then almost a ‘working experiment’ to mess around with to get RT from trial onset to start of the key press to work!

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [October 26, 2022, 3:19pm UTC](https://discourse.psychopy.org/t/code-component-for-additional-data-columns-not-working-in-pavlovia-but-does-in-builder/31574/11 "2022-10-26T15:19:15Z")

</div>

> [@ajus](#):
>
> ```auto
> for (var i in keys2) {
> key = keys2[i];
> 
> ```

I’ve not very good at reading JS code, but this looks like the first iteration of this look would set

```auto
key = keys2[keys2[0]];

```

which isn’t going to work.

You could try replacing it with

`for (key in keys2) {`

However, when I Auto translate that I get

```auto
for (var key, _pj_c = 0, _pj_a = keys2, _pj_b = _pj_a.length; (_pj_c < _pj_b); _pj_c += 1) {
    key = _pj_a[_pj_c];

```

It’s much easier if you use Auto translate components whenever possible

[https://psychopy.org/online/psychoJSCodingDebugging.html](https://psychopy.org/online/psychoJSCodingDebugging.html)
