为什么我在R中使用写函数时得到问号?

问题描述 投票:0回答:1

我有以下代码,我创建了一个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)
}

由于某些原因,"车主的 "一词中的""被一个? "代替了。 起初我以为这与''有关,但没有''也会出现同样的问题。

例子二。

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

有什么想法为什么会发生这种情况,或者如何修复它?

r tidyverse roxygen2
1个回答
2
投票

@MacOS似乎对编码的看法是正确的。 虽然我不确定到底是什么造成了这个问题,但似乎我已经解决了这个问题。

我把 writestringi::stri_write_lines.


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.

© www.soinside.com 2019 - 2024. All rights reserved.