首页 > 解决方案 > 在 R 中获取函数时在控制台中静音或抑制文本

问题描述

我正在使用reticulate从 python 文件中获取函数reticulate::source_python()。但是,当我第一次在会话中获取它时,这会在 RStudio 控制台中以红色打印文本(警告);我想沉默或压制它。我尝试了几种方法,但都没有奏效:

 # Attempt 1
  suppressWarnings("My source_python(path_to_function)")
  
  # Attempt 2
  suppressMessages("My source_python(path_to_function)")
  
  # Attempt 3
  capture.output("My source_python(path_to_function)")
  
  # Attempt 4
  invisible("My source_python(path_to_function)")
  
  # Attempt 5
  invisible(capture.output("My source_python(path_to_function)"))
  
  # Attempt 6
  zz <- file("test.txt", open = "wt")
  sink(zz ,type = "output")
  sink(zz, type = "message")
  print("using print")
  cat("using cat\n")
  message("using message")
  warning("using warning")
  "My source_python(path_to_function)"
  #and to close connections
  sink()
  sink()
  file.show("test.txt")

有没有其他方法可以尝试。提前致谢。

标签: rwarningsreticulate

解决方案


推荐阅读