在R中使用write函数时,为什么会出现问号?

时间:2020-05-23 10:43:21

标签: 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)
}

由于某种原因?替换“所有者”一词中的“。最初,我认为它与'有关,但是没有'我会遇到同样的问题。

示例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)
}

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

2 个答案:

答案 0 :(得分:2)

@MacOS在编码方面似乎是正确的。虽然我不确定到底是什么造成了该问题,但看来我已经解决了该问题。

我从write更改为stringi::stri_write_lines

答案 1 :(得分:0)

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

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