(我不确定这是 r 还是 shell 问题,请原谅添加两个标签,如果您认为我应该删除一个,请发表评论,我会这样做(
我在 rstudio.example.com 有一个亚马逊托管的 r 版本。我已经编写了两个脚本,当我从 Rstudio 界面中获取它们时,它们都运行良好。
当我 ssh 进入我的脚本目录并从那里运行时,脚本会生成一些错误。
第一个脚本的目的是对数据框中的文本列进行 qdap::check_spelling,然后获取该拼写错误的频率以及拼写错误的单词示例:
library(tidyverse)
library(qdap)
# example data
exampledata <- data.frame(
id = 1:5,
text = c("cats dogs dgs cts oranges",
"orngs orngs cats dgs",
"bannanas, dogs",
"cats cts dgs bnnanas",
"ornges fruit")
)
# check for unique misspelt words using qdap
all.misspelts <- check_spelling(exampledata$text) %>% data.frame %>% select(row:not.found)
unique.misspelts <- unique(all.misspelts$not.found)
# for each misspelt word, get the first instance of it appearing for context/example of word in a sentence
contexts.misspellts.index <- lapply(unique.misspelts, function(x) {
filter(all.misspelts, grepl(paste0("\b",x,"\b"), not.found))[1, "row"]
}) %>% unlist
# join it all together in a data farem to write to a csv
contexts.misspelts.vector <- exampledata[contexts.misspellts.index, "text"]
freq.misspelts <- table(all.misspelts$not.found) %>% data.frame() %>% mutate(Var1 = as.character(Var1))
misspelts.done <- data.frame(unique.misspelts, contexts.misspelts.vector, stringsAsFactors = F) %>%
left_join(freq.misspelts, by = c("unique.misspelts" = "Var1")) %>% arrange(desc(Freq))
write.csv(x = misspelts.done, file="~/csvs/misspelts.example_data_done.csv", row.names=F, quote=F)
最终数据框如下所示:
> print(misspelts.done)
unique.misspelts contexts.misspelts.vector Freq
1 dgs cats dogs dgs cts oranges 3
2 cts cats dogs dgs cts oranges 2
3 orngs orngs orngs cats dgs 2
4 bannanas bannanas, dogs 1
5 bnnanas cats cts dgs bnnanas 1
6 ornges ornges fruit 1
当我在 RStudio 的云实例上运行它时,它运行没有问题,并且在最后一行代码指定的目录中生成一个 csv 文件。
当我在 linux 中运行它时,我得到:
myname@ip-10-0-0-38:~$ r myscript.R
ident, sql
During startup - Warning message:
Setting LC_CTYPE failed, using "C"
During startup - Warning message:
Setting LC_CTYPE failed, using "C"
During startup - Warning message:
Setting LC_CTYPE failed, using "C"
During startup - Warning message:
Setting LC_CTYPE failed, using "C"
During startup - Warning message:
Setting LC_CTYPE failed, using "C"
During startup - Warning message:
Setting LC_CTYPE failed, using "C"
During startup - Warning message:
Setting LC_CTYPE failed, using "C"
During startup - Warning message:
Setting LC_CTYPE failed, using "C"
Error in grepl(paste0("\b", x, "\b"), not.found) :
object 'not.found' not found
In addition: Warning message:
In data.matrix(data) : NAs introduced by coercion
myname@ip-11-0-0-28:~/rscripts$
看起来我的grepl()
功能有问题。但是在 Rstudio 中运行时它工作正常,只是从 shell 调用脚本时则不行。
但是我也在基于 dplyry 动词(过滤器(的单独脚本中遇到了其他错误。
如果有人认识到这个问题,请帮助!如果需要更多信息,请告诉我,我会补充。
附言我尝试在本地运行 shell 中的脚本,它起作用了。这可能是我的亚马逊服务器的问题吗?
shell 中的文件:
shell$ r < input.R > output.CSV
我不确定是否适用于 R。 你可以试试!
通过反复试验,我发现在每个函数的库名称前面加上前缀解决了这个问题,例如dplyr::select()
.我不知道为什么,但我希望我能理解。这只需要在从ssh r myscript.R.
调用脚本时完成 在我测试的所有其他环境中,情况并非如此,包括本地终端、本地 RStudio 实例、托管 RStudio 实例 - 所有 3 个都不需要我预置库,只有在通过 ssh 调用时