2017-11-11 16 views
0

我试图从python运行R脚本。我可以看到它正在执行,但是Python对R脚本的语法错误进行了错误处理。 R脚本在RStudio中运行正常,所以我不确定问题可能是什么。任何的意见都将会有帮助。R脚本中的数据强制转换Python语法错误问题

这是我的Python代码:

import sys 
import subprocess 

# other stuff going on 

# execute R script which returns a document in a directory 
subprocess.call([sys.executable, "C:/Users/path/sentiment_extraction.r"]) 

sentiment = pd.read_csv("new_sentiment_data.csv") 

这里是我的[R脚本:

#! /usr/bin/Rscript 
require(syuzhet) 

today_news <- read.csv("C:/Users/path/Today_News.csv") # created from above python script 

#Sentiments words table: 
records <- as.character(today_news$News) 
sentiment <- get_nrc_sentiment(records) 

# other stuff that makes a file 

那么这是Python的错误,我得到:

File "C:/Users/path/sentiment_extraction.r", line 7

records <- as.character(today_news$News) 
      ^

SyntaxError: invalid syntax

的数据我使用syuzhet需要是一个字符向量,所以我投了列。

+0

你能尝试打印'STR(today_news)' – akrun

+0

today_news是一个文件。当我使用today_news $ News时,这是R用于获取文件中的'News'列中的数据today_news ... –

+0

我明白这一点,它是检查'str(today_news)'是否有任何不同这个方法和直接读取R – akrun

回答

0

我想通了。我运行R文件的参数不正确。我不得不路径传递到Rscript.exe:

subprocess.call(["C:/Users/seeme/Documents/R/R-3.4.0/bin/Rscript", "C:/Users/path/sentiment_extraction.r"])