Page 1 of 1

YAMOVI returns NA from function mean(..

Posted: Sun Nov 15, 2020 12:16 pm
by Hey-Guy
Using the barplot.b.R und barplot.a.yaml below, YAMOVI returns NA.

# This file is a generated template, your changes will not be overwritten

BarplotClass <- if (requireNamespace('jmvcore')) R6::R6Class(
"BarplotClass",
inherit = BarplotBase,
private = list(
.run = function() {

mittel = mean(self$options$dep)
self$results$text$setContent(mittel)
})
)

---
name: Barplot
title: Test Barplot
menuGroup: Sonntag
version: '1.0.0'
jas: '1.2'

options:
- name: data
type: Data

- name: dep
title: Dependent Variable
type: Variable

Re: YAMOVI returns NA from function mean(..

Posted: Sun Nov 15, 2020 10:21 pm
by jonathon
hi,

the variable `dep` is defined as type `Variable`, so it will come through as a string (aka a character vector of length 1). taking a mean of a string will generate an NA. you want to perform a mean on the data, rather than the column name, like this:

var <- self$data[[self$options$dep]])
mittel <- mean(var)

or:

mittel <- mean(self$data[[self$options$dep]])

or ideally:

var <- jmvcore::toNumeric(self$data[[self$options$dep]]))
mittel <- mean(var)

makes sense?

(i can invite you to the slack group if you like, you may find that an easier format for getting help on module development).

cheers

jonathon