R-nvim / R.nvim

Neovim plugin to edit R files
GNU General Public License v3.0
178 stars 18 forks source link

nvimcom:::source.and.clean inconsistently needs plot() #204

Closed aavogt closed 2 months ago

aavogt commented 3 months ago

If I send the png / ggplot / dev.off() lines with <localleader>l, it correctly makes the h.png. If I send the whole file I don't get the h.png (ie. "h.png: cannot open `h.png' (No such file or directory)"). If I add plot( ggplot ... ) or remove some of the apparently unnecessary lines, I get the h.png.

library(tidyverse)
library(ggplot2)

# df <- Sys.glob("*.csv") %>%
#   map_dfr(~ {
#     cbind(f = ., read_csv(., col_names = c("T", "h")))
#   })
# 
# # cat(deparse(dir()))
# ns <- c("10kPa.csv" = 10, "150kPa.csv" = 150, "18e-3kPa.csv" = 18e-3, "220kPa.csv" = 220, "80.5kPa.csv" = 80.5)
# df$p <- ns[df$f]
# write_csv(df, "df.csv")
df <- read_csv("df.csv")

png(file="h.png", width = 400, height = 200)
ggplot(df, aes(p, h, col = T)) +
  ylab("h / (W/m²K)") +
  xlab("p / kPa") +
  geom_point() +
  theme_minimal() + 
  ggtitle("h = a * p^b")
dev.off()
print(system("file h.png", intern = T))
system("rm h.png")

df.csv

f,T,h,p
10kPa.csv,41.13252472675314,4.575119307705421,10
PMassicotte commented 3 months ago

Using source all on this works for me (ie. h.png is created)

library(ggplot2)

png(file = "h.png", width = 400, height = 200)
ggplot(mtcars, aes(x = mpg)) +
  geom_histogram()

dev.off()
jalvesaq commented 3 months ago

I confirm the bug. I get the expected output after <LocalLeader>aa with @PMassicotte's code, but not with @aavogt's one. The code of nvimcom:::source.and.clean is:

function (f, ...) {
    on.exit(unlink(f))
    base::source(f, ...)
}

I don't know what might be wrong...

PMassicotte commented 3 months ago

Interesting, removing the comments will make it work:

library(tidyverse)
library(ggplot2)

# df <- Sys.glob("*.csv") %>%
#   map_dfr(~ {
#     cbind(f = ., read_csv(., col_names = c("T", "h")))
#   })
#
# # cat(deparse(dir()))
# ns <- c("10kPa.csv" = 10, "150kPa.csv" = 150, "18e-3kPa.csv" = 18e-3, "220kPa.csv" = 220, "80.5kPa.csv" = 80.5)
# df$p <- ns[df$f]
# write_csv(df, "df.csv")

df <- tribble(
  ~f, ~T, ~h, ~p,
  "10kPa.csv", round(41.13252472675314, 2), round(4.575119307705421, 2), 10
)

png(file = "h.png", width = 400L, height = 200L)
ggplot(df, aes(p, h, col = T)) +
  ylab("h / (W/m²K)") +
  xlab("p / kPa") +
  geom_point() +
  theme_minimal() +
  ggtitle("h = a * p^b")
dev.off()
print(system("file h.png", intern = T))
PMassicotte commented 3 months ago

But this is working

library(tidyverse)
library(ggplot2)

# df <- Sys.glob("*.csv") %>%
#   map_dfr(~ {
#     cbind(f = ., read_csv(., col_names = c("T", "h")))
#   })
#

png(file = "h.png", width = 400L, height = 200L)
ggplot(mtcars, aes(x = mpg)) +
  geom_histogram()
dev.off()

print(system("file h.png", intern = TRUE))
system("rm h.png")
PMassicotte commented 3 months ago

Note the difference in the comment. Looks like a base R issue?

jalvesaq commented 3 months ago

I agree that it seems to be an issue with base::source. The results are identical if we run base::source() manually on the R Console.

jalvesaq commented 3 months ago

We need a minimal example to put in the Known bugs section and close this issue.

jalvesaq commented 2 months ago

@aavogt, could you please try this in your R.nvim config:

    source_args = "print.eval = TRUE",

Your issue seems to be the opposite of #216

jalvesaq commented 2 months ago

From source help:

Note that running code via source differs in a few respects from entering it at the R command line. Since expressions are not executed at the top level, auto-printing is not done. So you will need to include explicit print calls for things you want to be printed (and remember that this includes plotting by lattice, FAQ Q7.22).

It seems that we should set print.eval=TRUE by default as we have done for nvimcom:::Rnvim.source.

PMassicotte commented 2 months ago

It seems that we should set print.eval=TRUE by default as we have done for nvimcom:::Rnvim.source.

Would make sens.

jalvesaq commented 2 months ago

@aavogt, could you try the print_eval branch, please?