将文本文件导入R并将其更改';s格式

将文本文件导入R并将其更改';s格式,r,R,我有一个文本文件中的数据。文本文件的示例如下所示: "vLatitude ='23.8145833'; vLongitude ='90.4043056'; vcontents ='LRP: LRPS</br>Start of Road From the End of Banani Rail Crossing Over Pass</br>Division:Gazipur</br>Sub-Division:Tongi';

我有一个文本文件中的数据。文本文件的示例如下所示:

"vLatitude ='23.8145833';       
vLongitude ='90.4043056';
vcontents ='LRP: LRPS</br>Start of Road From the End of Banani Rail Crossing Over Pass</br>Division:Gazipur</br>Sub-Division:Tongi';
                
vLocations = new Array(vcontents, vLatitude, vLongitude);   
locations.push(vLocations);"
解决方案1 这看起来很像javascript代码。执行javascript(使用web浏览器),然后用R和
jsonlite
打开文件

根据您的示例,创建此文件并将其另存为
my_page.html

<html>
<header>
<script>

// Initialize locations to be able to push more values in it
// probably not required with your full code
var locations = [];

vLatitude ='23.8145833';       
vLongitude ='90.4043056';
vcontents ='LRP: LRPS</br>Start of Road From the End of Banani Rail Crossing Over Pass</br>Division:Gazipur</br>Sub-Division:Tongi';
                
vLocations = new Array(vcontents, vLatitude, vLongitude);   
locations.push(vLocations);

// convert locations to json
var jsonData = JSON.stringify(locations);

// actually write the json to file
function download(content, fileName, contentType) {
    var a = document.createElement("a");
    var file = new Blob([content], {type: contentType});
    a.href = URL.createObjectURL(file);
    a.download = fileName;
    a.click();
}
download(jsonData, 'export_json.txt', 'text/plain');


</script>
</header>
<body>
Download should start automatically. You can look at the web console for errors.
</body>
</html>
一个问题是javascript代码是由一个没有名称的数组创建的。因此,不会导出名称。我不明白你怎么能让javascript导出它

解决方案2 不必依赖浏览器来执行javascript代码,您可以直接在R中使用。它应该会给你同样的结果,但会使两者之间的沟通更容易

解决方案3 如果文件一直都是这样的,那么您可以删除组织数组的javascript行,而只保留定义变量的行。在R中,符号
=
在技术上是有效的,将javascript重写为R代码并不难。注意,根据javascript代码中的其他内容,此解决方案可能非常脆弱

js_script <- "var locations = [];

vLatitude ='23.8145833';       
vLongitude ='90.4043056';
vcontents ='LRP: LRPS</br>Start of Road From the End of Banani Rail Crossing Over Pass</br>Division:Gazipur</br>Sub-Division:Tongi';
                
vLocations = new Array(vcontents, vLatitude, vLongitude);   
locations.push(vLocations);

// convert locations to json
var jsonData = JSON.stringify(locations);" %>%
  str_split(pattern = "\n", simplify=TRUE) %>%
  as.character() %>%
  str_trim()


# Find the lines that look like defining variables
js_script <- js_script[str_detect(js_script, pattern = "^\\w+ ?= ?'.*' ?;$")]

# make it into an R expression
r_code <- str_remove(js_script, ";$") %>%
  paste(collapse = ",")

r_code <- paste0("c(", r_code, ")")

# Execute
eval(str2expression(r_code))
js_脚本%
str_split(pattern=“\n”,simplify=TRUE)%
as.character()%>%
str_trim()
#查找看起来像定义变量的行
js_脚本
jsonlite::read_json("export_json.txt",simplifyVector = TRUE)
js_script <- "var locations = [];

vLatitude ='23.8145833';       
vLongitude ='90.4043056';
vcontents ='LRP: LRPS</br>Start of Road From the End of Banani Rail Crossing Over Pass</br>Division:Gazipur</br>Sub-Division:Tongi';
                
vLocations = new Array(vcontents, vLatitude, vLongitude);   
locations.push(vLocations);

// convert locations to json
var jsonData = JSON.stringify(locations);" %>%
  str_split(pattern = "\n", simplify=TRUE) %>%
  as.character() %>%
  str_trim()


# Find the lines that look like defining variables
js_script <- js_script[str_detect(js_script, pattern = "^\\w+ ?= ?'.*' ?;$")]

# make it into an R expression
r_code <- str_remove(js_script, ";$") %>%
  paste(collapse = ",")

r_code <- paste0("c(", r_code, ")")

# Execute
eval(str2expression(r_code))