Page 1 of 1

descriptives function in R - can't return table or plots

Posted: Wed Mar 10, 2021 3:33 am
by Andrius_M
Hello jamovi users!

New user here - I am trying to use jvm package in R.

I have written a function that takes lists of variables and a histogram T/F argument.
The function works, but I can not get the results out.
I would like to return the descriptives table and histogram plots back and render to html via rmarkdown.

Things that did not work:
- wrapping the descriptives in print() or show()
- print(results$descriptives) inside the for loop and outside of it
- return(results$descriptives)

The errors I am getting are: object 'results' not found or similar

here is my function code:

Code: Select all

func_DescriptivesPlots  <- function(df, anova_str, histogram_on){
  # get the number of Descriptives analyses from # of rows
  len <- length(anova_str)  

  for(i in seq(1,len)){
    
    # get variable (column) names from list of strings
    descr_list <- unlist(anova_str[[i]])
    var <- names(df)[descr_list]
    target<- var[2:length(var)]

    # jvm function
    descriptives(df[target],
                 missing = FALSE,
                 sd = TRUE,
                 se = FALSE,            
                 freq = FALSE,
                 hist = histogram_on
    )
    
  }
  return(results$descriptives) # error!
  return(results$plots)	     # error!
}

Re: descriptives function in R - can't return table or plots

Posted: Wed Mar 10, 2021 4:05 am
by jonathon
hi,

notice that you are calling the descriptives() function, but you don't actually assign the results of the descriptives function to an object called 'results'.

try:

results <- descriptives(df[target], ...

cheers

jonathon

Re: descriptives function in R - can't return table or plots

Posted: Wed Mar 10, 2021 4:17 am
by Andrius_M
Thanks Jonathon!

This actually works, when I then do print(results) inside the for loop.

Two follow-up questions, if you have some thoughts on this:

1) in descriptives table is there away to change "Standard Deviation" to "SD" and limit the digits of values for compactness?

2) is there a way to easily wrap histogram plots into figures with panels such as 3x2, etc?

Re: descriptives function in R - can't return table or plots

Posted: Wed Mar 10, 2021 4:22 am
by jonathon
the jamovi analyses honour the digits setting of R, which you can change with:

options(digits=3)

https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/options

there is an easy way to wrap plots in panels, but i don't know it :P

it's possible maurizio will chime in, he knows about that stuff.

cheers

jonathon

Re: descriptives function in R - can't return table or plots

Posted: Mon Mar 15, 2021 10:11 pm
by MAgojam
Andrius_M wrote:in descriptives table is there away to change "Standard Deviation" to "SD" and limit the digits of values for compactness?
hi,
see if this interests you:

Code: Select all

library(jmv)

options(digits = 3)
data('mtcars')
my_df   <- mtcars
my_vars <- colnames(my_df)[c(1, 3:4)]

results <- jmv::descriptives(data    = my_df[my_vars],
                             hist    = TRUE,
                             n       = TRUE,
                             missing = FALSE,
                             mean    = TRUE,
                             sd      = TRUE,
                             se      = FALSE,
                             median  = TRUE,
                             min     = FALSE,
                             max     = FALSE,
                             pc      = FALSE)

results$descriptives
# Replace the default "Standard Deviation" with "SD"
results$descriptives$setCell(col = 29, value = "SD", rowNo=1)
results$descriptives
# Replace the default  title "Descriptives" with "My Descriptive Analysis"
results$setTitle("My Descriptive Analysis") 
results$descriptives$analysis

p1 <- results$plots[[1]] 
p2 <- results$plots[[2]]
p3 <- results$plots[[3]]

ls.str(results)
Cheers,
Maurizio

Re: descriptives function in R - can't return table or plots

Posted: Tue Mar 16, 2021 2:09 am
by Andrius_M
Thank you Jonathon and Maurizio, very helpful!