表达式无法在函数内找到对象

发布于 2025-01-13 05:11:32 字数 1004 浏览 1 评论 0原文

如果在函数外部运行,下面的代码可以正常工作 - 所有内容都被正确评估,并且比较云可以转换为 ggplot。但是,当我想将其作为函数运行时,表达式无法再找到函数内定义的变量(例如,term.matrix)。

我已经尝试了一系列与 expression() bquote() expr() 等的组合,但未能找到解决方案。

谁能帮助我吗?

library(tm)
library(ggplotify)
library(wordcloud)
library(ggplot2)

cloud_as_ggplot <- function(){
  
  data(SOTU)
corp <- SOTU
corp <- tm_map(corp, removePunctuation)
corp <- tm_map(corp, content_transformer(tolower))
corp <- tm_map(corp, removeNumbers)
corp <- tm_map(corp, function(x)removeWords(x,stopwords()))

term.matrix <- TermDocumentMatrix(corp)
term.matrix <- as.matrix(term.matrix)
colnames(term.matrix) <- c("SOTU 2010","SOTU 2011")

cloud <- expression(
  comparison.cloud(term.matrix,
                   max.words=40,
                   random.order=FALSE,
                   match.colors=TRUE))

title <- "as.ggplot is working"

ggplotify::as.ggplot(cloud) + 
  labs(title = title)
}

cloud_as_ggplot()

The code below works fine if ran outside the function - everything is being evaluated correctly, and the comparison cloud can be converted to a ggplot. However, when I want to run this as a function, the expression can no longer find the variables that are defined inside the function (e.g., the term.matrix).

I've tried a bunch of combinations with expression() bquote() expr() etc., but have not been able to find the solution.

Can anyone help me?

library(tm)
library(ggplotify)
library(wordcloud)
library(ggplot2)

cloud_as_ggplot <- function(){
  
  data(SOTU)
corp <- SOTU
corp <- tm_map(corp, removePunctuation)
corp <- tm_map(corp, content_transformer(tolower))
corp <- tm_map(corp, removeNumbers)
corp <- tm_map(corp, function(x)removeWords(x,stopwords()))

term.matrix <- TermDocumentMatrix(corp)
term.matrix <- as.matrix(term.matrix)
colnames(term.matrix) <- c("SOTU 2010","SOTU 2011")

cloud <- expression(
  comparison.cloud(term.matrix,
                   max.words=40,
                   random.order=FALSE,
                   match.colors=TRUE))

title <- "as.ggplot is working"

ggplotify::as.ggplot(cloud) + 
  labs(title = title)
}

cloud_as_ggplot()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

稀香 2025-01-20 05:11:32

这是旧的,但我有一个类似的问题,最终发现了这个: https: //github.com/GuangchuangYu/ggplotify/issues/6#issuecomment-533442229,这为我解决了这个问题。

以下是上面的代码,无需向全局环境分配任何内容:

library(tm)
#> Loading required package: NLP
library(ggplotify)
library(wordcloud)
#> Loading required package: RColorBrewer
library(ggplot2)
#> 
#> Attaching package: 'ggplot2'
#> The following object is masked from 'package:NLP':
#> 
#>     annotate

cloud_as_ggplot <- function(){
  
  data(SOTU)
  corp <- SOTU
  corp <- tm_map(corp, removePunctuation)
  corp <- tm_map(corp, content_transformer(tolower))
  corp <- tm_map(corp, removeNumbers)
  corp <- tm_map(corp, function(x)removeWords(x,stopwords()))
  
  term.matrix <- TermDocumentMatrix(corp)
  term.matrix <- as.matrix(term.matrix)
  colnames(term.matrix) <- c("SOTU 2010","SOTU 2011")
  
  title <- "as.ggplot is working"
  
  ggplotify::as.ggplot(
    function() comparison.cloud(term.matrix, max.words=40, random.order=FALSE, match.colors=TRUE)
  ) + 
    labs(title = title)
}

cloud_as_ggplot()

This is old, but I had a similar question and ultimately came upon this: https://github.com/GuangchuangYu/ggplotify/issues/6#issuecomment-533442229, which solved it for me.

Here is the above code working without assigning anything to the global environment:

library(tm)
#> Loading required package: NLP
library(ggplotify)
library(wordcloud)
#> Loading required package: RColorBrewer
library(ggplot2)
#> 
#> Attaching package: 'ggplot2'
#> The following object is masked from 'package:NLP':
#> 
#>     annotate

cloud_as_ggplot <- function(){
  
  data(SOTU)
  corp <- SOTU
  corp <- tm_map(corp, removePunctuation)
  corp <- tm_map(corp, content_transformer(tolower))
  corp <- tm_map(corp, removeNumbers)
  corp <- tm_map(corp, function(x)removeWords(x,stopwords()))
  
  term.matrix <- TermDocumentMatrix(corp)
  term.matrix <- as.matrix(term.matrix)
  colnames(term.matrix) <- c("SOTU 2010","SOTU 2011")
  
  title <- "as.ggplot is working"
  
  ggplotify::as.ggplot(
    function() comparison.cloud(term.matrix, max.words=40, random.order=FALSE, match.colors=TRUE)
  ) + 
    labs(title = title)
}

cloud_as_ggplot()

忘年祭陌 2025-01-20 05:11:32

我认为这是与 Comparison.cloud 获取参数的环境有关的问题。它似乎是 .GlobalEnv 而不是由 cloud_as_ggplot() 创建的。

您可以通过将 term.matrix 设置为全局变量来“修复”它,最后您可以删除它。对于我来说,它有点脏,但很有效。

library(tm)
library(ggplotify)
library(wordcloud)
library(ggplot2)

cloud_as_ggplot <- function(){
  
  data(SOTU)
  corp <- SOTU
  corp <- tm_map(corp, removePunctuation)
  corp <- tm_map(corp, content_transformer(tolower))
  corp <- tm_map(corp, removeNumbers)
  corp <- tm_map(corp, function(x)removeWords(x,stopwords()))
  
  term.matrix <<- TermDocumentMatrix(corp)
  term.matrix <<- as.matrix(term.matrix)
  colnames(term.matrix) <- c("SOTU 2010","SOTU 2011")
  
  cloud <- expression(
    comparison.cloud(term.matrix,
                     max.words=40,
                     random.order=FALSE,
                     match.colors=TRUE))
  
  
  
  title <- "as.ggplot is working"
  p<-ggplotify::as.ggplot(cloud) + 
    labs(title = title)
  rm(term.matrix,envir=.GlobalEnv)
  p
}

cloud_as_ggplot()

I think it is an issue about the environment from which comparison.cloud is taking the parameters. It seems to be .GlobalEnv and not the one created by cloud_as_ggplot().

You can "fix" it by making term.matrix a global variable and at the end you can delete it. For my it is kind of dirty but it works.

library(tm)
library(ggplotify)
library(wordcloud)
library(ggplot2)

cloud_as_ggplot <- function(){
  
  data(SOTU)
  corp <- SOTU
  corp <- tm_map(corp, removePunctuation)
  corp <- tm_map(corp, content_transformer(tolower))
  corp <- tm_map(corp, removeNumbers)
  corp <- tm_map(corp, function(x)removeWords(x,stopwords()))
  
  term.matrix <<- TermDocumentMatrix(corp)
  term.matrix <<- as.matrix(term.matrix)
  colnames(term.matrix) <- c("SOTU 2010","SOTU 2011")
  
  cloud <- expression(
    comparison.cloud(term.matrix,
                     max.words=40,
                     random.order=FALSE,
                     match.colors=TRUE))
  
  
  
  title <- "as.ggplot is working"
  p<-ggplotify::as.ggplot(cloud) + 
    labs(title = title)
  rm(term.matrix,envir=.GlobalEnv)
  p
}

cloud_as_ggplot()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文