Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I very much like the purpose of this plug-in #3

Closed
erex opened this issue Nov 28, 2024 · 37 comments
Closed

I very much like the purpose of this plug-in #3

erex opened this issue Nov 28, 2024 · 37 comments

Comments

@erex
Copy link

erex commented Nov 28, 2024

I write R-code and Quarto documents almost daily. Same for Obsidian; appreciate effort bring them together.

However, I cannot make the plugin work on my Windows 11 machine. I have installed previous versions of the plugin and today installed version 0.1.1. I'm using it with R 4.4.2 and Quarto 1.6.38. I am confident I have properly specified paths (and consulted issue #1 closed two weeks ago.

C:\Program Files\R\R-4.4.2\bin\x64\R.exe
C:\Program Files\quarto\bin\quarto.exe

I don't need htmlwidgets at the moment, so not interested in Pandoc, but have configured it nonetheless
C:\Program Files\Pandoc\pandoc.exe

Working with a simple file with a single code chunk to create a histogram,
image

this is what the console shows:
image

Attempting a Quarto html render of the same file produces this at the console
image

Below is raw file:

---
title: Ridian2
format: html
date: 2024-11-28
author: Eric
---
# Second attempt
Simply trying to get simple R code to function

```r
#| label: 4e074085
hist(runif(1000))
```

The above should have produced a histogram of uniformly distributed random variates.

You can see Ridian adds a label to the code chunk, but nothing else.

I appreciate your help.

@MichelNivard
Copy link
Owner

Hi Eric, took me a while to debug! So the quarto doc you share didnt run for me either, while others did. The label needs to be a string, which it is (see the is an e in there) but its not recognised as such by quarto, I think that might actually be a quarto bug so I'll open an issue with quarto and implement a quick patch for Ridian. This leaves R not running. The console is being crowded by to many logs from the autocomplete code, that's totally my fault, ill patch the logging so we can figure out the other issue for you.

@erex
Copy link
Author

erex commented Nov 28, 2024

very nice of you to have a look at my dual problems. I'm running fairly new release of Quarto, maybe that's part of the story. Standing by to hear more about the patch to the logging

@MichelNivard
Copy link
Owner

for us to confirm the quarto issue is really this issue while we wait for the quarto ppl to respond, can you try and render with "hello" as the label (no quotes)? if that works then i can focus on the other issue

@erex
Copy link
Author

erex commented Nov 28, 2024

Your experiment with hello as the label, does indeed succeed in rendering an HTML file in my Obsidian/exports folder
image

@MichelNivard
Copy link
Owner

are the following R packages installed on your machine?

install.packages(evaluate)
install.packages(jsonlite)
install.packages(htmlwidgets)
install.packages(languageserver)

@MichelNivard
Copy link
Owner

note language server is new I hadnt updated the readme.md, just the website.

@erex
Copy link
Author

erex commented Nov 28, 2024

No, I was missing evaluate and languageserver

I've installed those packages, closed and reopened Obsidian, but still Ridan doesn't run the code chunk when asked.

Does it matter if RStudio is running at the same time, I presume Ridian creates a new instance of R for its own purposes.

@MichelNivard
Copy link
Owner

It certainly has its own instance yes. Do you get autocomplete/function suggestions when the cursor is within a function like the one I pasted here:

image

@erex
Copy link
Author

erex commented Nov 28, 2024

yes, I do get autocomplete suggestions in a new chunk that I created within the same Obsidian document. When I hit Run chunk I get a label manufactured, but no output.

A side note: Your step 5 about installing packages missed my attention on first reading. This is because it was down below BRAT instructions. My brain shut off when I encountered BRAT instructions because I (incorrectly) assumed everything following that was out-of-date now that the plugin has been approved and BRAT was no longer necessary. Simply my habits as an impatient reader.

@MichelNivard
Copy link
Owner

So R works for sure, otherwise there is no autocomplete. can you try and run code that outputs text but not a plot?

‘’’r
1+2
‘’’

does it output a result?

Can you ask for a help page:

‘’’r
?hist()
‘’’

typing on my phone these might not be backticks but u think you get the point.

@erex
Copy link
Author

erex commented Nov 28, 2024

Understand your instructions,

No luck on either count
image

You're very tenacious about this.

@MichelNivard
Copy link
Owner

Thanks this is the info I needed to narrow down where the issue lies, I have a win 11 test setup I can see about reproducing and then fixing the issue. Can you share the other plugins you use, these things can sometimes be plugin conflicts…

@erex
Copy link
Author

erex commented Nov 29, 2024

Thanks Professor. I have quite a large assortment (67) of installed plugins.

I don't know of a text-based way to show installed plugins, using this clumsy method
image
image

@MichelNivard
Copy link
Owner

So, I installed those that are available on the plugin "store", simply enabling those on win 11, then disabling and enabling Ridian I cant reproduce your failure. Given many of these plugins aren't on the store (you might have gotten them elsewhere, or they aren't being maintained anymore). I will fix the issue with the autocomplete excessively logging so you can at least see whats going on while the plugin tries to run R code and we can see if there is some kind of Ridian problem I can solve, but I cant realistically test for all possible plugin conflicts. Given R runs, and the autocomplete works, perhaps its some kind of callout (results should be printed to a callout)/code related plugin getting in the way? You can expect the update that fixes the console being flooded to be life before Monday.

@erex
Copy link
Author

erex commented Nov 29, 2024

Understand you can't test for all conflicts. My plugin addiction is a bit extreme. I will try to do some plugin pruning at the weekend, and will let you know if there are useful developments. Thanks again for your help.

@erex
Copy link
Author

erex commented Nov 29, 2024

I too will leave this issue for the weekend. I have disabled (not uninstalled) all plugins with the exception of Ridian and still am unable to get an R code chunk to run.

The console does show this complaint regarding sqlite and the Zotero plugin (even with the plugin disabled). Afraid that is all the news I can report
image

@MichelNivard
Copy link
Owner

hm, so ifyou try to "run" a chunk either with the button or with cntrl-r, what appears in the console? try an make sure your mouse isnt in a chunk so we get all messages without the autocomplete crouding the console.

@erex
Copy link
Author

erex commented Dec 4, 2024

I've updated to v 0.1.2 of the plugin, tried to run a code chunk using the hotkey, with the mouse outside the chunk. I confess to being a poor debugging assistant, because the console receives no entries when the hotkey is pressed
image

@MichelNivard
Copy link
Owner

So those are other plugin's error, tell you what Ill try to add a log thta checks whether the code actually 1. goes to R and 2. comes back from R to track this bug.
.

@MichelNivard
Copy link
Owner

I pushed a silent update (no new version number) if you delete ad then re-install Ridian there should be better logging, if youd share that wiht me I can determin more precisely where the error arises.

@erex
Copy link
Author

erex commented Dec 4, 2024

I take it you mean install the silent version via BRAT? Looking at your repo, I see no new commits since ~0900 this morning.

I'll ask brat to install and report back to you.

@MichelNivard
Copy link
Owner

Plgins are installed form the releases not the repo (which I just updated) but BRAT should work just as well! In fact BRAT might work batter no idea if the Obsidian plugin repo instantly syncs...

@erex
Copy link
Author

erex commented Dec 4, 2024

OK, uninstalled and installed via BRAT; re-configured paths to three executables. Console does now echo considerable R code (too much to fit in a single screen). However, nothing is happening within the Obsidian document; no results of the code chunk is shown.

Content of console

sending the following code to R process: 
library(evaluate)
library(jsonlite)
library(htmlwidgets)
Sys.setenv(RSTUDIO_PANDOC='C:\Program Files\Pandoc\pandoc.exe')


    opts <- list(
      echo = TRUE,
      warning = TRUE,
      error = TRUE,
      include = TRUE,
      output = TRUE
    )
    

custom_print_htmlwidget <- function(x, ..., viewer = NULL) {
    widgetFileName <- paste0("widget_4e074085_",".html")
    widgetFilePath <- file.path("C:\\Windows\\Temp\\rplots-5embVC", widgetFileName)
    saveWidget(x, widgetFilePath, selfcontained = TRUE)
    cat("__WIDGET_PATH__", widgetFileName, "\n", sep="")
}

environment(custom_print_htmlwidget) <- asNamespace('htmlwidgets')
assignInNamespace("print.htmlwidget", custom_print_htmlwidget, envir = as.environment("package:htmlwidgets"))

timecheck <- Sys.time()

.getHelpFile <- function(file)
{
    path <- dirname(file)
    dirpath <- dirname(path)
    if(!file.exists(dirpath))
        stop(gettextf("invalid %s argument", sQuote("file")), domain = NA)
    pkgname <- basename(dirpath)
    RdDB <- file.path(path, pkgname)
    if(!file.exists(paste(RdDB, "rdx", sep = ".")))
        stop(gettextf("package %s exists but was not installed under R >= 2.10.0 so help cannot be accessed", sQuote(pkgname)), domain = NA)
    tools:::fetchRdDB(RdDB, basename(file))
}
print.help_files_with_topic <- function(x, ...)
{
  browser <- getOption("browser")
  topic <- attr(x, "topic")
  type <- "text"
  paths <- as.character(x)
  
  if(!length(paths)) {
    writeLines(c(gettextf("No documentation for %s in specified packages and libraries:",
                          sQuote(topic)),
                 gettextf("you could try %s",
                          sQuote(paste0("??", topic)))))
    return(invisible(x))
  }
  
  port <- NULL
  
  if(attr(x, "tried_all_packages")) {
    paths <- unique(dirname(dirname(paths)))
    msg <- gettextf("Help for topic %s is not in any loaded package but can be found in the following packages:",
                    sQuote(topic))
    
      writeLines(c(strwrap(msg), "",
                   paste(" ",
                         formatDL(c(gettext("Package"), basename(paths)),
                                  c(gettext("Library"), dirname(paths)),
                                  indent = 22))))
    } else {
    if(length(paths) > 1L) {
      file <- paths[1L]
      p <- paths
      msg <- gettextf("Help on topic %s was found in the following packages:",
                      sQuote(topic))
      paths <- dirname(dirname(paths))
      txt <- formatDL(c("Package", basename(paths)),
                      c("Library", dirname(paths)),
                      indent = 22L)
      writeLines(c(strwrap(msg), "", paste(" ", txt), ""))
      if(interactive()) {
        fp <- file.path(paths, "Meta", "Rd.rds")
        tp <- basename(p)
        titles <- tp
        if(type == "html" || type == "latex")
          tp <- tools::file_path_sans_ext(tp)
        for (i in seq_along(fp)) {
          tmp <- try(readRDS(fp[i]))
          titles[i] <- if(inherits(tmp, "try-error"))
            "unknown title" else
              tmp[tools::file_path_sans_ext(tmp$File) == tp[i], "Title"]
        }
        txt <- paste0(titles, " {", basename(paths), "}")
        ## the default on menu() is currtently graphics = FALSE
        res <- menu(txt, title = gettext("Choose one"),
                    graphics = getOption("menu.graphics"))
        if(res > 0) file <- p[res]
      } else {
        writeLines(gettext("
Using the first match ..."))
      }
    }
    else
      file <- paths
    
    if(type == "text") {
      pkgname <- basename(dirname(dirname(file)))
      tools::Rd2HTML(.getHelpFile(file), out = "C:/Windows/Temp/rplots-5embVC/help_4e074085.html",
                            package = pkgname)

    }
    
  }
  
  invisible(x)
}

if (!exists("user_env")) {
  user_env <- new.env()
}

results <- evaluate("cos(pi/2)", envir = user_env)

outputs <- character()
imagePaths <- character()

for (res in results) {
  if (inherits(res, "source")) {
  } else if (inherits(res, "warning")) {
    if (opts$warning && opts$include) {
      outputs <- c(outputs, paste("Warning:", conditionMessage(res)))
    }
  } else if (inherits(res, "message")) {
    if (opts$output && opts$include) {
      outputs <- c(outputs, res$message)
    }
  } else if (inherits(res, "error")) {
    if (opts$error && opts$include) {
      outputs <- c(outputs, paste("Error:", conditionMessage(res)))
    }
  } else if (inherits(res, "character")) {
    if (opts$output && opts$include) {
      outputs <- c(outputs, res)
    }
  } else if (inherits(res, "recordedplot")) {
    if (opts$output && opts$include) {
      timestamp <- format(Sys.time(), "%Y%m%d%H%M%S")
      plotFileName <- paste0("plot_4e074085_", length(imagePaths) + 1, "_", timestamp, ".jpg")
      plotFilePath <- file.path("C:\\Windows\\Temp\\rplots-5embVC", plotFileName)
      jpeg(filename=plotFilePath, width=800, height=600)
      replayPlot(res)
      dev.off()
      imagePaths <- c(imagePaths, plotFileName)
    }
  }
}

if (opts$output && opts$include) {
  if (requireNamespace("gganimate", quietly = TRUE)) {
    anim <- try(gganimate::last_animation(), silent = TRUE)
    if (is.character(anim[1])) {
      if (file.info(anim[1])$mtime > timecheck) {
        timestamp <- format(Sys.time(), "%Y%m%d%H%M%S")
        animFileName <- paste0("animation_4e074085_", timestamp, ".gif")
        animFilePath <- file.path("C:\\Windows\\Temp\\rplots-5embVC", animFileName)
        file.copy(anim[1], animFilePath)
        imagePaths <- c(imagePaths, animFileName)
      }
    }
  }
}

if (opts$output && opts$include && length(outputs) > 0) {
  cat(paste(outputs, collapse = "\n"), "\n")
}

if (opts$output && opts$include) {
  for (img in imagePaths) {
    cat("__PLOT_PATH__", img, "\n", sep="")
  }
}

vars <- ls(envir = user_env)
env_list <- lapply(vars, function(var_name) {
  var_value <- get(var_name, envir = user_env)
  var_class <- class(var_value)
  var_size <- as.numeric(object.size(var_value))
  var_val <- capture.output(str(var_value, max.level=0))

  list(
    name = var_name,
    type = var_class,
    size = var_size,
    value = var_val
  )
})

env_json <- toJSON(env_list, auto_unbox = TRUE)
cat("__ENVIRONMENT_DATA__1733335392821__\n")
cat(env_json)
cat("\n__END_OF_OUTPUT__1733335392821__\n")

when trying to execute this code chunk

#| label: 4e074085
cos(pi/2)

@erex
Copy link
Author

erex commented Dec 4, 2024

Pleased to see another user has reported this problem; I feel less a fool.

Thanks for your time diagnosing this problem.

@MichelNivard
Copy link
Owner

No log about trying to produce output after that long log with the r code?!

@erex
Copy link
Author

erex commented Dec 4, 2024

That's the end of the log, nothing else.

@MichelNivard
Copy link
Owner

Okay, so I apologize for what I am to try and put you trough... could you try an run that R code in your R install? It'll probably give errors because it's being executed in a very unintended way, but one potential issue is that maybe that code somehow isn't as robust as I thought it was!

@MichelNivard
Copy link
Owner

Some of the file paths are formatted poorly, I'll def fix those... no idea why my windows just accepts those...

@erex
Copy link
Author

erex commented Dec 4, 2024

Happy to help with progress on this.

New developments

Running Obsidian console code in my R install flagged up a "user error" on my part:

I had failed to use double-backslashes in specifying location of executables:

Sys.setenv(RSTUDIO_PANDOC='C:\\Program Files\\Pandoc\\pandoc.exe')

I changed this in Ridian and found new output in the console window (still nothing in output):

    
plugin:ridian:209 Uncaught (in promise) TypeError: fe.updateEnvironmentData is not a function
    at Socket.ce (plugin:ridian:209:2057)
    at Socket.emit (node:events:519:28)
    at addChunk (node:internal/streams/readable:559:12)
    at readableAddChunkPushByteMode (node:internal/streams/readable:510:3)
    at Readable.push (node:internal/streams/readable:390:5)
    at Pipe.onStreamRead (node:internal/stream_base_commons:191:23)
ce @ plugin:ridian:209
emit @ node:events:519
addChunk @ node:internal/streams/readable:559
readableAddChunkPushByteMode @ node:internal/streams/readable:510
Readable.push @ node:internal/streams/readable:390
onStreamRead @ node:internal/stream_base_commons:191

@erex
Copy link
Author

erex commented Dec 4, 2024

Looking inside my R Global Environment, I find the calculation I requested (cos(pi/2)) has indeed been evaluated, just not spit out:

results <- evaluate("cos(pi/2)", envir = user_env)

image

@erex
Copy link
Author

erex commented Dec 4, 2024

@MichelNivard Stopping for the night. Let me know how else I can be of assistance tomorrow.

@MichelNivard
Copy link
Owner

Okay this is good stuff... the R code runs without further error in R? I'll track down the new console error you got after changing the path.

@erex
Copy link
Author

erex commented Dec 4, 2024

no, no further errors

@MichelNivard
Copy link
Owner

So this error "TypeError: fe.updateEnvironmentData is not a function" could suggest you might not have en "environment" tab, are you seeing this tab on the right when ridian is activated?
image

@erex
Copy link
Author

erex commented Dec 5, 2024

What just happened?

I went looking for the Environment tab, which indeed is present on the right panel (which I never have open).

I opened the right panel to see if the tab was present, then used ctrl-r to try to run the chunk again, and this time I met with success:

image

From what I can tell, the Ridian plugin now appears to work as intended. I'm fairly sure I tried it this morning without success; but now (1245) it appears to be happy. Did BRAT do an update while I was working on other things?

@MichelNivard
Copy link
Owner

High fives all around! So what I think happens, is there were a few sequential issues we worked out, like fixing paths etc. These eventually reveal a bug in the plugin launch sequence that mean that in very specific obsidian editor states (I.e when the right panels are closed on initial app launch I think) the plugin doesn't register that tab properly and sends R output that way without checking whether there is a tab to receive it. You "solved" the bug by opening the tab... anyway this will mean I can target a full fix that shouldn't land ppl in this particular grey area...

@erex
Copy link
Author

erex commented Dec 5, 2024

Goodness, such a tangled set of circumstances leading to the problems. Thanks for your patience and diligence in hunting down the gremlins.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants