为什么我在 R 中使用写入函数时会出现问号?

  • 本文关键字:函数 r tidyverse roxygen2
  • 更新时间 :
  • 英文 :


我有以下代码来组成文档,并为API提供函数。 我在代码中使用write时遇到问题。 代码的第三行最终添加了问号。

例:

test_function <- structure(c("#' Add/Drops", "#'", "#' Retrieve a list of add/drops for the owner’s team. The list will include the following.", 
"#'", "#' @export", "#' @importFrom httr POST GET add_headers content warn_for_status", 
"#' @importFrom jsonlite fromJSON", "add_drops <- function() {", 
"  params <- list(", "    response_format = "JSON",", "    version = "3.0",", 
"    access_token = cbs_token", "  )", "", "  params <- no_null(params)", 
"  res <- httr::GET(", "    url = "http://api.cbssports.com/fantasy/league/transaction-list/add-drops",", 
"    query = params", "  )", "", "  warn_for_status(res)", "  jsonlite::fromJSON(httr::content(res, "text", encoding = "UTF-8"), flatten = TRUE)", 
"}"), class = "vertical")
test_function 
#' Add/Drops
#'
#' Retrieve a list of add/drops for the owner’s team. The list will include the following.
#'
#' @export
#' @importFrom httr POST GET add_headers content warn_for_status
#' @importFrom jsonlite fromJSON
add_drops <- function() {
params <- list(
response_format = "JSON",
version = "3.0",
access_token = cbs_token
)
params <- no_null(params)
res <- httr::GET(
url = "http://api.cbssports.com/fantasy/league/transaction-list/add-drops",
query = params
)
warn_for_status(res)
jsonlite::fromJSON(httr::content(res, "text", encoding = "UTF-8"), flatten = TRUE)
}

上面的代码看起来完全符合我希望它查找.R文件。

现在,当我使用write时出现问题

title <- "test_function"
write(test_function, sprintf("R/%s.R", title))
#' Add/Drops
#'
#' Retrieve a list of add/drops for the owner?s team. The list will include the following.
#'
#' @export
#' @importFrom httr POST GET add_headers content warn_for_status
#' @importFrom jsonlite fromJSON
add_drops <- function() {
params <- list(
response_format = "JSON",
version = "3.0",
access_token = cbs_token
)
params <- no_null(params)
res <- httr::GET(
url = "http://api.cbssports.com/fantasy/league/transaction-list/add-drops",
query = params
)
warn_for_status(res)
jsonlite::fromJSON(httr::content(res, "text", encoding = "UTF-8"), flatten = TRUE)
}

出于某种原因,a ? 替换了"所有者的"一词中的 '。 最初我认为这与"有关,但是在没有"的情况下我遇到了同样的问题。

示例 2:

test_function2 <- structure(c("#' Sports", "#'", "#' A list of the official CBS Interacive ID codes for sports. These are the sport IDs that must be used when requesting any FOPE API resource that requires the SPORT parameter. The resource contains a list of all the pro sports for which CBS Interactive a fantasy sports program. For each sport, it provides the offical CBS Interactive ID for the sport, a common English name for the sport, and an abbreviation for the sport.", 
"#'", "#' @export", "#' @importFrom httr POST GET add_headers content warn_for_status", 
"#' @importFrom jsonlite fromJSON", "sports <- function() {", 
"  params <- list(", "    response_format = "JSON",", "    version = "3.0",", 
"    access_token = cbs_token", "  )", "", "  params <- no_null(params)", 
"  res <- httr::GET(", "    url = "http://api.cbssports.com/fantasy/sports",", 
"    query = params", "  )", "", "  warn_for_status(res)", "  jsonlite::fromJSON(httr::content(res, "text", encoding = "UTF-8"), flatten = TRUE)", 
"}"), class = "vertical")
test_function 2
#' Sports
#'
#' A list of the official CBS Interacive ID codes for sports. These are the sport IDs that must be used when requesting any FOPE API resource that requires the SPORT parameter. The resource contains a list of all the pro sports for which CBS Interactive a fantasy sports program. For each sport, it provides the offical CBS Interactive ID for the sport, a common English name for the sport, and an abbreviation for the sport.
#'
#' @export
#' @importFrom httr POST GET add_headers content warn_for_status
#' @importFrom jsonlite fromJSON
sports <- function() {
params <- list(
response_format = "JSON",
version = "3.0",
access_token = cbs_token
)
params <- no_null(params)
res <- httr::GET(
url = "http://api.cbssports.com/fantasy/sports",
query = params
)
warn_for_status(res)
jsonlite::fromJSON(httr::content(res, "text", encoding = "UTF-8"), flatten = TRUE)
}

这是该.R文件的外观。

#' Sports
#'
#' A list of the official CBS Interacive ID codes for sports.? These are the sport IDs that must be used when requesting any FOPE API resource that requires the SPORT parameter.? The resource contains a list of all the pro sports for which CBS Interactive a fantasy sports program.? For each sport, it provides the offical CBS Interactive ID for the sport, a common English name for the sport, and an abbreviation for the sport.
#'
#' @export
#' @importFrom httr POST GET add_headers content warn_for_status
#' @importFrom jsonlite fromJSON
sports <- function() {
params <- list(
response_format = "JSON",
version = "3.0",
access_token = cbs_token
)
params <- no_null(params)
res <- httr::GET(
url = "http://api.cbssports.com/fantasy/sports",
query = params
)
warn_for_status(res)
jsonlite::fromJSON(httr::content(res, "text", encoding = "UTF-8"), flatten = TRUE)
}

知道为什么会发生这种情况,或者如何解决它吗?

@MacOS编码似乎是正确的。 虽然我不确定究竟是什么导致了这个问题,但似乎我已经解决了这个问题。

我从write变成了stringi::stri_write_lines.

我认为这是因为您使用的是'而不是'。

owner’s

owner's

编辑

两个版本在我的计算机上都可以正常工作。

dput(R.version)
structure(
list(
platform = "x86_64-conda_cos6-linux-gnu",
arch = "x86_64", 
os = "linux-gnu",
system = "x86_64, linux-gnu", 
status = "", 
major = "3",
minor = "6.1",
year = "2019",
month = "07", 
day = "05",
`svn rev` = "76782",
language = "R",
version.string = "R version 3.6.1 (2019-07-05)", 
nickname = "Action of the Toes"), class = "simple.list"))

也许您以错误的编码保存了文件?我确实用了UTF-8.

相关内容

  • 没有找到相关文章

最新更新