Module that can add new columns

Everything related to the development of modules in jamovi
Post Reply
imad19
Posts: 4
Joined: Thu Mar 13, 2025 12:13 am

Module that can add new columns

Post by imad19 »

Hello !

Is there any possibility to create a module for Jamovi that can automatically create new columns , it might create 1 or 3 or 7 it depends ...

Is it possible ?

Thank you.
User avatar
jonathon
Posts: 2798
Joined: Fri Jan 27, 2017 10:04 am

Re: Module that can add new columns

Post by jonathon »

Hi,

yes it is, although it's not extremely well documented.

what does the number of columns depend on? it's easy if there's a simple formula for it, i.e. there are as many columns as there are variables specified by the user ... but if the number of columns is more complex, i.e. determined by an analysis, and not known in advance, then that is a bit trickier.

what's your situation?

jonathon
imad19
Posts: 4
Joined: Thu Mar 13, 2025 12:13 am

Re: Module that can add new columns

Post by imad19 »

Hi Jonathon,

Thanks for your quick response!

I’m currently working on a module that can split a variable. In certain cases, I have a variable with values like "a,b,c", and I want to automatically split this into three new columns. Each new column should be named based on the original column name followed by the value (e.g., "original_name_a"). The values in these new columns should be either 1 or 0, depending on whether the value is present.

To keep things simple, could you provide a basic code snippet (for b.R) that will create just one new column for testing purposes? I tried implementing it myself, but it didn’t work.
User avatar
jonathon
Posts: 2798
Joined: Fri Jan 27, 2017 10:04 am

Re: Module that can add new columns

Post by jonathon »

ah, righto ... it sounds a lot like you're implementing dummy coding :P

this is the more difficult one to implement ... but it's not too bad.

step 1. add an output results to your .r.yaml, something like:

Code: Select all

    - name: factorScoresOV
      title: Factor scores
      type: Output
      initInRun: true
https://github.com/jamovi/jmv/blob/22c9 ... #L201-L210

step 2. add a matching option to your .a.yaml to activate it:

Code: Select all

    - name: factorScoresOV
      title: Component scores
      type: Output
https://github.com/jamovi/jmv/blob/22c9 ... #L169-L171

step 3.

setup the columns:

Code: Select all

self$results$factorScoresOV$set(
    keys=keys,
    titles=titles,
    descriptions=descriptions,
    measureTypes=measureTypes
)
keys, titles, descriptions, measureTypes should all be character vectors of the same length (as many columns as you want). i.e.

Code: Select all

self$results$factorScoresOV$set(
    keys=c('1', '2'),
    titles=c('column 1', 'column 2'),
    descriptions=c('this column represents', '... and this column represents'),
    measureTypes=c('continuous', 'continuous')
)
step 4. populate the data:

something like:

Code: Select all

for (i in 1:self$nFactors) {
    scores <- as.numeric(self$scores[, i])
    self$results$factorScoresOV$setValues(index=i, scores)
}
...

here's an example from the PCA.

https://github.com/jamovi/jmv/blob/22c9 ... #L440-L474

come back to us if you get stuck.
imad19
Posts: 4
Joined: Thu Mar 13, 2025 12:13 am

Re: Module that can add new columns

Post by imad19 »

Thank you, will give it a try Tomorrow and tell you the results
imad19
Posts: 4
Joined: Thu Mar 13, 2025 12:13 am

Re: Module that can add new columns

Post by imad19 »

Thank Jonathon for your guidance.

I've finalized the module for splitting combined data columns into separate columns. I have a question regarding distribution—how can I make this module accessible to everyone?

Looking forward to your advice.
User avatar
jonathon
Posts: 2798
Joined: Fri Jan 27, 2017 10:04 am

Re: Module that can add new columns

Post by jonathon »

if you place it under an open-source license, list in a public git repo (i.e. github), we can look at listing it in the jamovi library.

if that's where you want to go, submit it, we'll take a look, make some suggestions, and after a bit of back and forth we list it in the library.

jonathon
Post Reply