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

General help and assistance with jamovi. Bug reports can be made at our issues page: https://github.com/jamovi/jamovi/issues . (If you're unsure feel free to discuss it here)
Post Reply
Andrius_M
Posts: 3
Joined: Wed Mar 10, 2021 3:11 am

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

Post 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!
}
User avatar
jonathon
Posts: 2617
Joined: Fri Jan 27, 2017 10:04 am

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

Post 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
Andrius_M
Posts: 3
Joined: Wed Mar 10, 2021 3:11 am

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

Post 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?
User avatar
jonathon
Posts: 2617
Joined: Fri Jan 27, 2017 10:04 am

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

Post 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
User avatar
MAgojam
Posts: 421
Joined: Thu Jun 08, 2017 2:33 pm
Location: Parma (Italy)

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

Post 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
Andrius_M
Posts: 3
Joined: Wed Mar 10, 2021 3:11 am

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

Post by Andrius_M »

Thank you Jonathon and Maurizio, very helpful!
Post Reply