Page 1 of 2
display values from dataframe into row data
Posted: Wed Sep 25, 2024 4:44 am
by lkchan
Hi
Is it possible to for loop the value from data frame into a table?
Code: Select all
table1 <- self$results$scoreTable
for (name in self$options$dep){
table1$addColumn(name, title=name)
}
for(value in prpRev_df){
table1$setRow(rowNo=1, value=list(
var = "Experts in Agreement",
list = values
))
}
I try to loop it but not success.
https://github.com/lerlerchan/cviJmv/tree/main
Re: display values from dataframe into row data
Posted: Wed Sep 25, 2024 5:04 am
by jonathon
try something like this instead:
for (rowNo in seq_len(nrow(prpRev_df))) {
row <- as.list(prpRev_df[rowNo, , drop=TRUE]
row$var <- "Experts in Agreement"
table1$setRow(rowNo=rowNo, values=row)
}
i *think* that will work
jonathon
Re: display values from dataframe into row data
Posted: Wed Sep 25, 2024 7:38 am
by lkchan
hi after checking the structure of the dataframe. There is only 1 row withe many columns.
This code work.
Code: Select all
for (colNo in seq_along(I_CVI_values_df)){
values <- as.list(I_CVI_values_df[colNo])
table1$setRow(rowNo = 2, values)
}
table1$setRow(rowNo = 2, list(var = paste("I-CVI")))
This code doesn't work??? Same data frame structure
Code: Select all
for (colNo in seq_along(UA_values_df)){
values <- as.list(UA_values_df[colNo])
table1$setRow(rowNo = 3, values)
}
table1$setRow(rowNo = 3, list(var = paste("Universal Agreement (UA)")))
I want to display the data in vertical format. It doesnt work too.
Code: Select all
#output proportion relevance of experts in table
table3 <- self$results$propTable
for (colNom in seq_along(prpRev_df)) {
if (colNom > table3$rowCount)
break()
values <- as.list(prpRev_df[colNom])
table3$setRow(rowNo=colNom, values)
table3$setRow(rowNo=colNom, list(var=paste("Expert ", colNom)))
}
Please guide me.
Thank you
Re: display values from dataframe into row data
Posted: Wed Sep 25, 2024 8:11 am
by jonathon
so are we still working with the scoreTable table? because that only has a single column.
maybe the first step is to draw a sketch of what you want the final table to look like, and then show us the structure of the data frame that you're wanting to populate that table from. if we have those two pieces of information, i can give you advice on how to map between the two.
so maybe just draw it on a piece of paper, photograph it, and post it here.
cheers
jonathon
Re: display values from dataframe into row data
Posted: Thu Sep 26, 2024 2:40 am
by lkchan
Re: display values from dataframe into row data
Posted: Fri Sep 27, 2024 2:14 am
by jonathon
hey,
OK, so these data frames are 1 row, many columns ... so we can iterate across the columns with seq_along() (which you've done above).
Code: Select all
for (colNo in seq_along(UA_values_df)) {
var <- paste("Expert", colNo)
value <- UA_values_df[1,colNo,drop=TRUE]
row <- list(var=var, value=value)
table1$setRow(rowNo = colNo, values=row)
}
note that this assumes that your table has at least two columns (one called "var" and one called "value" ... you can change these to suit), and that your table has enough rows to accommodate all the values.
let me know how you get on.
jonathon
Re: display values from dataframe into row data
Posted: Mon Sep 30, 2024 7:23 am
by lkchan
Hi
Regarding table, when the all values in dataframe is 0, why it is not display? so I created a dummy dataframe but it is not display.
Code: Select all
# Define the number of rows and columns
num_rows <- 1
num_cols <- ncol(transformed_df)
# Create a DataFrame filled with 0.000
df <- data.frame(matrix(rep(0.000, num_rows * num_cols), nrow = num_rows, ncol = num_cols))
# Set column names (optional)
colnames(df) <- paste0("Column", 1:num_cols)
if (is.null(UA_values_df)) {
self$results$text$setContent("NULL")
for (colNo in seq_along(UA_values_df)) {
values <- as.list(UA_values_df[colNo])
table1$setRow(rowNo = 3, values)
}
}else{
self$results$text$setContent("NOT NULL")
# Display 0.00 if UA_values_df is NULL
for (colNo in seq_along(df)) {
values <- as.list(df[colNo])
table1$setRow(rowNo = 3, values)
}
}
table1$setRow(rowNo = 3, list(var = paste("Universal Agreement (UA)")))
I am still debugging the third table
Re: display values from dataframe into row data
Posted: Mon Sep 30, 2024 8:15 am
by lkchan
Re: display values from dataframe into row data
Posted: Mon Sep 30, 2024 8:35 am
by jonathon
so there's two important things to check here. the first is that the table contains suitable columns, secondly that the items in the values list have names which match those columns.
in the following, i don't think the items in the list will have appropriate names:
Code: Select all
values <- as.list(UA_values_df[colNo])
i think all you might need to do is add a comma for this to work:
Code: Select all
values <- as.list(UA_values_df[colNo,])
however, this will be taking the names from the dataframe ... so your table will need to have the columns defined Column1, Column2, etc. (based on your code: colnames(df) <- paste0("Column", 1:num_cols)) .. we normally define columns in the .r.yaml file.
the other thing that stands out as being a little unusual is always setting the third row:
it's a little odd, but i suppose as long as your table has at least 3 rows, it should work.
jonathon
Re: display values from dataframe into row data
Posted: Tue Oct 01, 2024 2:17 am
by lkchan
hi Sir, I tried your suggestion. However it doesn't work. I tried to do it this way. It is not displayed too.
Code: Select all
df_name <- self$options$dep
combined_UA_df <-rbind(df_name, UA_values_df)
combined_UA_df <- as.matrix(combined_UA_df)
self$results$text$setContent(combined_UA_df)
for (colNo in seq_along(I_CVI_values_df)) {
values <- as.list(combined_UA_df[2, colNo])
table1$setRow(rowNo = 3, values)
}
table1$setRow(rowNo = 3, list(var = paste("Universal Agreement (UA)")))
is it jamovi v2.6.13 can't display 0 value?
