0

I have the following code that I've create to make up the documentation, and function for an API. I'm having issues when I use write with the code. The third line of the code ends up adding in question marks.

Example:

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

The following code above looks exactly as I want it to look for the .R file.

Now, the issue happens when I use 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)
}

For some reason a ? replaces the ' in the word "owner's". Initially I thought it had to do with the ', but I get the same issue without a '.

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

Here is what that .R file looks like.

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

Any idea why this would be happening, or how to fix it?

Jazzmatazz
  • 615
  • 7
  • 18

2 Answers2

2

@MacOS seems to have been correct about encoding. While I'm not sure exactly what was creating the issue, it seems I've fixed the issue.

I changed from write to stringi::stri_write_lines.

Jazzmatazz
  • 615
  • 7
  • 18
  • Great! Can you please update your original question with your system info (OS)? And the encoding you got on the file? That would greatly enhance the question as others can immediatly see what your question relates to. Furhter, please mark this as the correct answer so others can identify it as the solution (you will get a badge for that). Finally, consider giving my answer an Up Vote. Thank You! – MacOS May 23 '20 at 13:02
  • You can use `dput(R.version)` to get the system info. – MacOS May 23 '20 at 13:08
0

I think that is because your are using ´ instead of '.

owner’s

versus

owner's

EDIT

Both versions work fine on my computer.

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

Maybe you have saved the file in the wrong encoding? I did use UTF-8.

MacOS
  • 1,149
  • 1
  • 7
  • 14
  • It happens without ' as well. I'll amend my example. – Jazzmatazz May 23 '20 at 10:58
  • I’m pretty sure then that this is an encoding Issue. Can you please also add your encoding to the answer? – MacOS May 23 '20 at 11:01
  • How would I check the encoding? – Jazzmatazz May 23 '20 at 11:05
  • Which OS are you using? See [here for Linux](https://stackoverflow.com/questions/11018967/how-can-i-be-sure-of-the-file-encoding), and [here for Windows](https://stackoverflow.com/questions/3710374/get-encoding-of-a-file-in-windows). – MacOS May 23 '20 at 12:59