Getting data from RJeditor back to the jamovi dataset

Everything related to the development of modules in jamovi
Post Reply
Shabd
Posts: 3
Joined: Thu Dec 13, 2018 9:17 pm

Getting data from RJeditor back to the jamovi dataset

Post by Shabd »

Hi,
I love the RJ editor feature very much (you guys are doing a great job!), but I have one important question related to it:

It seems currently impossible to get data that was created in the RJeditor "system R sandbox" back to the Jamovi dataset. E.g. if i do:
data <-cbind(data, new_column)
...and then do:
descriptives (data)
... I can see the newly created column in the output, however, the new column is not appearing in the jamovi spreadsheet and hence cannot be saved with it.

Am I right that this is still impossible right now?
And if so, do you have a quick workaround how to make this work?

Such a function would be useful since I plan to do a simple statistic course with jamovi. Students will have no idea of R but in one course module might want to use some R-packages with functions that jamovi does not provide (like, e,g, poLCA) and take the generated data (e.g. class assignment) "back to jamovi" in order to do simple analyses with it there.
Any idea to achieve this in a very easy way?

Thanks a lot in advance, cheers,
Shabd
User avatar
jonathon
Posts: 2824
Joined: Fri Jan 27, 2017 10:04 am

Re: Getting data from RJeditor back to the jamovi dataset

Post by jonathon »

hi shabd,

you're right, this is a limitation of jamovi (and Rj) at this time.

as a work around, you can generate a stream of text values, and paste them into a column in jamovi:
Screen Shot 2018-12-14 at 09.49.09.png
Screen Shot 2018-12-14 at 09.49.09.png (225.25 KiB) Viewed 59561 times
what you copy/paste from the results (i notice) starts with a blank line, and pasting this doesn't work, so you have to paste the values into something like notepad, *then* copy all the values (skipping the first line), before you can paste those into a column in jamovi.

i'll fix this issue shortly.

jonathon
User avatar
credmontgo
Posts: 34
Joined: Tue Oct 12, 2021 1:31 pm
Location: Toronto, Canada
Contact:

Re: Getting data from RJeditor back to the jamovi dataset

Post by credmontgo »

Updates here, or better way?
RHainez
Posts: 105
Joined: Wed Feb 08, 2017 3:14 pm

Re: Getting data from RJeditor back to the jamovi dataset

Post by RHainez »

+1 on this feature
User avatar
jonathon
Posts: 2824
Joined: Fri Jan 27, 2017 10:04 am

Re: Getting data from RJeditor back to the jamovi dataset

Post by jonathon »

my plan is to have this sometime in june

jonathon
DeborahA
Posts: 24
Joined: Tue Apr 14, 2020 11:38 am

Re: Getting data from RJeditor back to the jamovi dataset

Post by DeborahA »

Is there any update on this? I have some students using some code I wrote a while ago to calculate Mahalanobis Distance, and they want to get the data back into the dataset.

Thanks!

Deborah.
User avatar
jonathon
Posts: 2824
Joined: Fri Jan 27, 2017 10:04 am

Re: Getting data from RJeditor back to the jamovi dataset

Post by jonathon »

hi, yes, if you use the Rj Editor+, you can do things like:

data <- cbind(data, values)

and it will be added into the data set.
User avatar
MAgojam
Posts: 446
Joined: Thu Jun 08, 2017 2:33 pm
Location: Parma (Italy)

Re: Getting data from RJeditor back to the jamovi dataset

Post by MAgojam »

Hey Deborah,
take a look at the screenshot, you might be interested in using Rj Editor+
Rj Editor+ Mahalanobis.png
Rj Editor+ Mahalanobis.png (449.78 KiB) Viewed 54402 times
with this code:

Code: Select all

MahalanobisOutlierDetector <- R6::R6Class("MahalanobisOutlierDetector",
  public = list(
    data = NULL,
    threshold = 5.0,
    mDist = NULL,  # Abbreviated name for Mahalanobis distances
    outliers = NULL,

    initialize = function(data, threshold = 5.0) {
      self$data <- data
      self$threshold <- threshold
      self$calculate_distances()
      self$identify_outliers()
    },

    calculate_distances = function() {
      # Calculate the Mahalanobis distances on the centered data
      self$mDist <- stats::mahalanobis(self$data,
                                       colMeans(self$data), stats::cov(self$data))

      # Add the Mahalanobis distances to the dataset
      self$data$mDist <- self$mDist
    },

    identify_outliers = function() {
      # Identify outliers based on the threshold
      self$data$outliers <- ifelse(self$data$mDist > self$threshold, 
                                   "True", 
                                   "False")
      self$outliers <- self$data[self$data$outliers == "True", ]
    },
    
    plot_qq = function() {
      ggplot2::ggplot(self$data, ggplot2::aes(x = mDist)) +
        ggplot2::geom_histogram(bins = 30, fill = 'blue', alpha = 0.7) +
        ggplot2::geom_vline(xintercept = self$threshold, 
                           color = 'red', 
                           linetype = 'dashed') +
        ggplot2::labs(title = 'Mahalanobis Distance Histogram',
                      x = 'Mahalanobis Distance',
                      y = 'Frequency') +
        ggplot2::theme_minimal()
    }
  )
)

# ---------------------------------------------------------
# Example usage with a dataset of 150 cases and 4 variables
set.seed(123) # For reproducibility
# Create an instance of the MahalanobisOutlierDetector class
detector <- MahalanobisOutlierDetector$new(data, 
                                           threshold = 5.0)
# Print only the outliers (rows where outliers == "True")
print(detector$outliers)

# Plot the Mahalanobis distance histogram with a threshold line
detector$plot_qq()

data <- cbind(detector$data)
Cheers,
Maurizio
https://www.jamovi.org/about.html
Post Reply