简体   繁体   English

R-saemixData函数在函数环境中找不到数据“文件错误(文件,“ rt”):无法打开连接”

[英]R - saemixData function do not find data in a function environment “Error in file(file, ”rt“) : cannot open the connection”

In R (version 3.3.2) I get the error "Error in file(file, "rt") : cannot open the connection" when I use the function saemixData inside a function. 在R(3.3.2版)中,当我在函数中使用函数saemixData时,出现错误“文件(文件,“ rt”)中的错误:无法打开连接”。 It is as if saemixData could not find the dataframe sim when sim is in the function environment. 当sim在函数环境中时,好像saemixData无法找到数据框sim。 The code works well when it is run outside of the function f, or when sim is in the global environment... It seems like a bug of the function saemixData, do you have a solution ? 当代码在函数f外部运行或sim在全局环境中运行时,该代码运行良好。似乎像saemixData函数的错误,您有解决方案吗?

Thanks ! 谢谢 !

rm(list=ls())
library(saemix)  # saemix_2.1.tar.gz


f=function(){

    sim = data.frame(patient=c(1,1,1,2,2,2),
                          time=c(1,4,8,1,4,8),
                          HBA1C_obs=c(9,8,7,8,7.5,6))   

    saemix.data <- saemixData(name.data       = sim,
                              name.group      = "patient",
                              name.predictors =  c("time"),
                              name.response   = "HBA1C_obs")  

    saemix.data
}


f()

# Reading data from file sim 
# Error in file(file, "rt") : cannot open the connection
# Error in read(x) : 
#   The file sim does not exist. Please check the name and path. 

I also have this same problem. 我也有同样的问题。 As a workaround you can assign sim to the global environment from within the function by using a double arrow: 解决方法是,您可以使用双箭头从函数中将sim分配给全局环境:

sim <<- data.frame(patient=c(1,1,1,2,2,2),
                   time=c(1,4,8,1,4,8),
                   HBA1C_obs=c(9,8,7,8,7.5,6))

But it's only a poor fix as it will overwrite any sim object in the global environment each time the function is called. 但这只是一个较差的解决方法,因为每次调用该函数时,它将覆盖全局环境中的所有sim对象。

yes I'm aware of this issue which you can chalk down to an insufficient understanding on my part of the environment issues in R. I wanted a function that could accept both objects in the environment and files on disk. 是的,我知道这个问题,您可以归结为对R中的环境问题没有足够的了解。我想要一个既可以接受环境中的对象又可以接受磁盘上的文件的函数。 I'm very sorry about this and if anyone has a better way of coding the function saemix.data please feel free to send it ! 对此我感到非常抱歉,如果有人能更好地对函数saemix.data进行编码,请随时发送!

Another workaround is to write data to a temporary file although this is clearly inefficient if you have a large file: 另一个解决方法是将数据写入临时文件,尽管如果您有一个大文件,这显然效率不高:

f=function(){
    sim = data.frame(patient=c(1,1,1,2,2,2),
                      time=c(1,4,8,1,4,8),
                      HBA1C_obs=c(9,8,7,8,7.5,6))
    tempname<-tempfile()
    write.table(sim,tempname,quote=F,col.names=T)
    saemix.data <- saemixData(name.data       = tempname,
                          name.group      = "patient",
                          name.predictors =  c("time"),
                          name.response   = "HBA1C_obs")
    saemix.data
}

f()

Emmanuelle 艾曼纽(Emmanuelle)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 文件错误(文件,“rt”):无法在 r 中打开连接 - Error in file(file, “rt”) : cannot open the connection in r 文件中的错误(文件,“rt”):无法打开连接另外:警告消息:在文件(文件,“rt”)中:无法打开文件 - Error in file(file, “rt”) : cannot open the connection In addition: Warning message: In file(file, “rt”) : cannot open file 文件中的错误(文件,“rt”):即使设置正确的路径,也无法在 r 中打开连接 - Error in file(file, "rt") : cannot open the connection in r even after setting right path arulesSequences cspade函数:“ file(con,“ r”)中的错误:无法打开连接” - arulesSequences cspade function: “Error in file(con, ”r“) : cannot open the connection” 文件错误(文件,“rt”):无法打开连接 - Shiny - Error in file(file, "rt") : cannot open the connection - Shiny 在文件(文件,“ rt”)中出现错误:无法打开连接 - Getting Error in file(file, “rt”) : cannot open the connection 文件错误(文件,“rt”):无法打开连接(寻找其他解决方案) - Error in file(file, "rt") : cannot open the connection (looking for another solution) 文件中出现错误(文件,“rt”):无法打开连接 - Getting error in file(file, "rt"): cannot open the connection JAGS,rjags:“文件错误(modfile,”rt“):无法打开连接” - JAGS, rjags: “Error in file(modfile, ”rt“) : cannot open the connection” 无法打开文件'test.csv':没有这样的文件或目录文件中的错误(文件,“rt”):无法打开连接 - cannot open file 'test.csv': No such file or directory Error in file(file, “rt”) : cannot open the connection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM