how to display dataframe data in columns?

Everything related to the development of modules in jamovi
lkchan
Posts: 9
Joined: Mon Dec 04, 2023 1:08 am

how to display dataframe data in columns?

Post by lkchan »

I wish to create a dynamic tables as Image

1) Is it possible to display a dataframe data in dynamic table?
2) can we loop through the column and dataframe?

Currently, I am trying to learn from https://github.com/arcaldwell49/SimplyAgree. I am able to display the data in rows.

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

Re: how to display dataframe data in columns?

Post by jonathon »

hi,

i'm afraid i don't quite understand what you mean. could you describe in a bit more detail?

with thanks
lkchan
Posts: 9
Joined: Mon Dec 04, 2023 1:08 am

Re: how to display dataframe data in columns?

Post by lkchan »

this is my r.yaml code

Code: Select all

    - name: scoreTable
      title: Fuzzy Score Table
      type: Table
      rows: 4
      columns:
      - name: var
        title: ''
        type: text
      - name: varScore
        title: ''
        type: number
here is the .b.R code

Code: Select all

 table1 <- self$results$scoreTable
              # Assuming you have a dataframe called 'dataframe' with the data you want to display
              
              # Initialize an empty list to store the values
              rows <- list()
              
              # Loop through each row of the dataframe
              for (i in 1:nrow(df4)) {
                # Extract the values from the dataframe
                value <- df4$value[i]
                description <- df4$description[i]
                
                # Append the values to the 'rows' list
                rows[[i]] <- list(
                  var = description,
                  varDconstruct = value
                )
              }

              
              # Loop through the 'rows' list and set each row in the table
              for (i in 1:length(rows)) {
                table1$setRow(rowNo = i, values = rows[[i]])
              }
              
              
            #output the calculated Info into dcTable            
              table2 <- self$results$dcTable
              #row1 = value d construct
              table2$setRow(rowNo=1, values=list(
                var = "Value d construct",
                varDconstruct = dConstruct
              ))
              #row2 = result percentage
              table2$setRow(rowNo=2, values=list(
                var = "%o f expert consesnsus for construct",
                varDconstruct = perCentageDCon
              ))
I want to display value from dataframe into all the columns cells in rowNo1. How to do the forloop? is it use addColumn functions?
User avatar
jonathon
Posts: 2627
Joined: Fri Jan 27, 2017 10:04 am

Re: how to display dataframe data in columns?

Post by jonathon »

hi,

taking a look at your code, i can't see anything obviously wrong with it, so i would expect it to work ... if it doesn't work, i assume there's something a bit subtle we're overlooking.

if you commit this module to a github repo, and share the link with us, someone should be able to figure out what's wrong.

cheers
lkchan
Posts: 9
Joined: Mon Dec 04, 2023 1:08 am

Re: how to display dataframe data in columns?

Post by lkchan »

Hi

https://github.com/lerlerchan/FuzzyDelpiJmv.git

still trying to understand addColumn() functions
User avatar
jonathon
Posts: 2627
Joined: Fri Jan 27, 2017 10:04 am

Re: how to display dataframe data in columns?

Post by jonathon »

do you have an example data set i can use with this?

(most modules won't need to use addColumn() because the .r.yaml file sets up the tables appropriately)

with thanks
lkchan
Posts: 9
Joined: Mon Dec 04, 2023 1:08 am

Re: how to display dataframe data in columns?

Post by lkchan »

https://github.com/lerlerchan/FuzzyDelp ... sampleData

Thank you

can setRow ... display data in columns cells? use for loop columns in the dataframe?
User avatar
jonathon
Posts: 2627
Joined: Fri Jan 27, 2017 10:04 am

Re: how to display dataframe data in columns?

Post by jonathon »

hi,

so the problem is the way you're reading values from the df4 data frame.

this code:

value <- df4$value
description <- df4$description

$value and $description don't exist in df4, and so these will just be assigned NULL.

to read the values from a data frame, you'll probably want to use something like:

df4[rowNumber, columnNumber]
df4[rowNumber, columnName]
df4[i, name]

if you can draw describe what you're hoping the final table looks like, i can probably point you in the right direction. i.e. are you after a table with 2 columns, 5 rows ... the first column contains Item 1, Item 2, Item 3, and the second column contains each of their scores?

jonathon
lkchan
Posts: 9
Joined: Mon Dec 04, 2023 1:08 am

Re: how to display dataframe data in columns?

Post by lkchan »

https://github.com/lerlerchan/FuzzyDelp ... Output.png

yes. a table with 5 rows and 6 columns


item1 item2 item3 item4 item5
________________________________________
title1 x x x x x #from df2
title2 x x x x x # from df4
title3
title4
User avatar
jonathon
Posts: 2627
Joined: Fri Jan 27, 2017 10:04 am

Re: how to display dataframe data in columns?

Post by jonathon »

still not completely clear to me what you're trying to do, but hopefully this can get you on the right track.

here you can see me populating the 'Fuzzy Score Table' from the data frame.
Screenshot 2024-02-20 at 18.23.28.png
Screenshot 2024-02-20 at 18.23.28.png (144.29 KiB) Viewed 425 times
to achieve this, i add a .init() function beside the .run() function that looks like this:

Code: Select all

        .init = function() {
          table1 <- self$results$scoreTable
          for (name in self$options$deps) {
            table1$addColumn(name, title=name)
          }
        },
then inside .run() i go:

Code: Select all

              for (rowNo in seq_len(nrow(rounded_dataframe))) {
                if (rowNo > table1$rowCount)
                  break()
                values <- as.list(rounded_dataframe[rowNo,])
                table1$setRow(rowNo=rowNo, values)
              }
hopefully there's enough information here for you to move forward.

your design is a little unusual, in that the number of columns in the table depends on the number of variables ... this is a less common design (the number of rows in a table matching the number of variables is far more common pattern), which is why you need to resort to an .init() function.

let me know if you'd like an invite to our slack group too. you may find that's an easier medium to ask these sorts of questions, etc.

cheers
Post Reply