YAMOVI returns NA from function mean(..

Everything related to the development of modules in jamovi

by Hey-Guy » Sun Nov 15, 2020 12:16 pm

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
Hey-Guy
 
Posts: 2
Joined: Thu Nov 12, 2020 3:19 pm

by jonathon » Sun Nov 15, 2020 10:21 pm

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


Return to Module development

cron