【问题标题】:Assign quanteda docvars programmatically with the function get()使用函数 get() 以编程方式分配 quanteda docvars
【发布时间】:2021-02-17 19:28:06
【问题描述】:

我正在开发一个例程来自动定义 quanteda 中的几个语料库。我有几个控制脚本的参数,其中之一是将要生成的语料库的名称。我可以使用函数assign() 以编程方式轻松创建语料库,但我完全无法向其中添加任何 docvars

一旦我定义了语料库,我通常会在整个代码中使用函数get() 调用它。我一直在成功地广泛使用这种方法。出于某种原因,函数docvars() 似乎不接受使用get() 调用的对象。

请看下面我定义语料库的简单代码,然后尝试将 docvar 与其关联。

library(quanteda)
#> Package version: 2.1.2
#> Parallel computing: 2 of 16 threads used.
#> See https://quanteda.io for tutorials and examples.
#> 
#> Attaching package: 'quanteda'
#> The following object is masked from 'package:utils':
#> 
#>     View

nameofthecorpus = "mycorpus"
mytext <- c( "This is a long speech made in 2020",
             "This is another long speech made in 2021")
thedocvars <- c( "2020", "2021" )

assign( nameofthecorpus, corpus( mytext ) )

# I can have a summary of the corpus with get()
summary( get( nameofthecorpus )  )
#> Corpus consisting of 2 documents, showing 2 documents:
#> 
#>   Text Types Tokens Sentences
#>  text1     8      8         1
#>  text2     8      8         1

# Now I wand to add some docvars automatically
# This is where I get stuck
docvars( get( nameofthecorpus ), "year" ) <- thedocvars 
#> Error in docvars(get(nameofthecorpus), "year") <- thedocvars: could not find function "get<-"

reprex package (v1.0.0) 于 2021-02-17 创建

原则上,我想一次将其推广到多个 docvar(例如,当它们存储在 data.frame 中时)。

有什么建议吗?

【问题讨论】:

    标签: r quanteda


    【解决方案1】:

    首先,我强烈建议您使用avoid get and assign when possible 来处理这样的变量。这是一种非常间接的方法,正如您已经看到的那样,在尝试使用这些间接值来更新值时很容易中断。当你运行类似

    docvars( mycorpus, "year" ) <- thedocvars 
    

    您正在运行一个名为docvars&lt;- 的特殊函数,该函数返回一个新对象,该对象将替换存储在mycorpus 中的值。当你输入get( nameofthecorpus )时,那不是一个可以替换的变量值,那是一个返回值的函数调用。所以如果你需要使用get/assign,你必须这样做

    assign(nameofthecorpus, `docvars<-`(get( nameofthecorpus ), "year", thedocvars))
    

    您是否从名称中检索值,显式调用 docvars 函数的转换版本以获取更新的对象值,然后将该值重新分配给原始变量名称。

    get/assign 的更好方法通常是命名列表。类似的东西

    nameofthecorpus = "mycorpus"
    mytext <- c( "This is a long speech made in 2020",
                 "This is another long speech made in 2021")
    thedocvars <- c( "2020", "2021" )
    
    mydata <- list()
    mydata[[nameofthecorpus]] <- corpus( mytext )
    summary( mydata[[nameofthecorpus]]  )
    docvars( mydata[[nameofthecorpus]], "year" ) <- thedocvars 
    

    【讨论】:

    • 谢谢!因此,没有直接的方法可以以编程方式创建对象并保留正确的类。我的意思是,我想在脚本末尾添加一个 corpus 对象。
    • 还有一点。做save( mydata[[ nameofthecorpus ]], file = "thepathtofile.RData" ) 不适用于object "mydata[[ nameofthecorpus ]]" not found。如何保存列表中定义的对象?
    • 我不清楚你在问什么。我不清楚你是如何使用这个“脚本”的。也许这一切都可以通过编写适当的函数来返回最终所需的对象而不是作为副作用注入到环境中来避免。
    • 如果您尝试保存单个对象,saveRDS 将是更好的选择。然后你根本不需要硬编码变量名。
    • 我正在使用 quanteda 中定义的标准函数。唯一的区别是我希望能够动态传递名称。你的建议效果很好。我可以毫无问题地访问所需类的对象,除非我想保存它们(请参阅上面的评论)。总而言之,我不想与脚本交互。这是通过Rscript 或通过 RStudio 中的 Jobs 启动的。谢谢
    猜你喜欢
    • 2018-11-08
    • 2011-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-12
    • 1970-01-01
    相关资源
    最近更新 更多