twibiral / obsidian-execute-code

Obsidian Plugin to execute code in a note.
MIT License
1.14k stars 67 forks source link

[Bug] Printing excess info in R #239

Open Pica-hudsonia opened 1 year ago

Pica-hudsonia commented 1 year ago

Hello! I've really been loving this plugin, but I've run into a consistent problem where my code will output things unnecessarily unless I add a print command at the end.

For instance, the following code outputs the dataframe as a vector:

library(tidyverse) # data cleaning
library(lmerTest)  # linear mixed effect package that has significance testing

DD <- read_csv("https://bodowinter.com/tutorial/politeness_data.csv", 
    col_types = cols(
        gender = col_factor(), 
        scenario = col_factor(),
        attitude = col_factor()
        )
    ) 

I am able to suppress the output by adding invisible(capture.output()) around the entire code creating the dataframe. However, in this chunk,

m2 <- lmer(frequency ~ attitude*gender + (1|subject)+ (1|scenario), data = DD)

summary(m2) |> print()

It will output the formatted results of the summary function and then just the unformatted summary function (see end of comment).

The easiest way I've found to fix this is to add print(" ") to the end of my code chunks, which prevents the unwanted information from being printed. Is there a better way to deal with this? Is this a genuine bug?

I'm on an M1 Pro Macbook running macOS 13.2.1

Problematic output example

Fixed effects:
                    Estimate Std. Error       df t value Pr(>|t|)    
(Intercept)          233.286     16.348    5.737  14.270 1.06e-05 ***
attitudeinf           27.400      7.791   69.017   3.517 0.000777 ***
genderM             -100.623     21.767    4.599  -4.623 0.007037 ** 
attitudeinf:genderM  -15.572     11.095   69.056  -1.403 0.164958    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Correlation of Fixed Effects:
            (Intr) atttdn gendrM
attitudeinf -0.238              
genderM     -0.663  0.179       
atttdnf:gnM  0.167 -0.702 -0.258
Linear mixed model fit by REML. t-tests use Satterthwaite's method lmerModLmerTest list(cmp = c(ldL2 = 25.9704349628346, ldRX2 = 6.54427940411381, wrss = 44914.3720838523, ussq = 5439.58476457183, pwrss = 50353.9568484241, drsum = NA, REML = 766.840388340023, dev = NA, sigmaML = 24.6307567666257, sigmaREML = 25.2466207410182), dims = c(N = 83, n = 83, p = 4, nmp = 79, q = 13, nth = 2, useSc = 1, reTrms = 2, spFe = 0, REML = 4, GLMM = 0, NLMM = 0)) TRUE TRUE -383.420194170012 NULL NULL c(scenario = 7, subject = 6) c(233.285714285718, ...
Josefmo commented 1 year ago

Pica-hudsonia, as an R user, I was wondering if you could help me out figuring out how to make the "Execute Code" plugin for Obsidian work. This may be a totally newbie question, but I'm not sure what I'm supposed to put into the "R arguments" settings. The plugin author encouraged me to check in what other R users were doing. I hope you don't mind.

mayurankv commented 1 year ago

Have you got an Rscript binary on your system? If that path (found using which Rscript) is put into the Rscript path in the settings, then running your R code should work. Is it something more advanced you're having problems with?

Pica-hudsonia commented 1 year ago

Hi @Josefmo! No worries! I'm still figuring this out myself.

I don't input anything into the "R arguments" section. I agree with the previous commentor, make sure you have R downloaded (https://www.r-project.org).

Next open up your terminal app and use where Rscript for macOS and Windows or which Rscript to find it on linux. Put that path into the "Rscript path" field in the R settings.

If that doesn't work, could you let me know what specific problem you're facing and the error you're getting? Also, what is your use case? The RStudio app is a really easy to use alternative if you are just looking to format and an run your code in a markdown formated file.