R error message for unused argument (writexl) - r

I have simple code
library(writexl)
write_xlsx(dataset, "C:/adhoc/my_data.xlsx")
The above works, however when I try
library(writexl)
write_xlsx(dataset, "C:/adhoc/my_data.xlsx", append=TRUE)
I get an unused argument error. How can I fix?

If you look at the documentation ?write_xlsx there is not an append argument defined for this function, thus the error. It has been raised as a possible feature for that package and deemed not possible. You could read the spreadsheet, do the appending in R, and then re-write the full version.
Alternatively, perhaps you would be served by write.xlsx() from the xlsx package which does have this argument and might be suitable for your purposes?

Related

R: Why does fs::dir_copy fail some of the time?

During a process, I need to copy a directory using fs::dir_copy(currentPath, newPath). Some of the time, I get the following error message:
Error in link_copy(links, path(new_path[[i]], path_rel(links, path[[i]])), :
all(is_link(path)) is not TRUE
This only happens some of the time, not always. What's more, if I replace my directory with a manual copy of itself (i.e. manually copy directory, delete original, rename the copy), then my code will work.
Could someone please explain why this could be happening? Is there a way I can sidestep that error once and for all?
This does not answer your question but may help you to further analyse what is going on. One of the great things of R is that you can easily inspect source code. Especially in a situation where unusual things appear to happen, looking at the code may be useful.
In your case, we can inspect the source code of fs::dir_copy and trace back to which code generates the error message. If you type fs::dir_copy (no parenthesis) into the console, R will print the R code of the function (if the function is not primitive). Doing that here will show you that the fs:dir_copy function calls the fs::link_copyfunction. Makes sense, as the error message comes from this function.
We can now print this function with fs::link_copy
This function generates the error message in this line:
stopifnot(all(is_link(path)))
From the error message we know that all(is_link(path)) returns FALSE. Next step is to have a look at the fs::is_link function. Here we see that the error may come from the call to the setNames function, which depends on the fs::file_info function: res <- file_info(path)
Here we see that setNames is called with a condition depending on what the file_infofunction returned:
setNames(!is.na(res$type) & res$type == "symlink", res$path)
This looks unusual as setNames takes an object and a character vector as formals. But then, I am not the developer of these functions ;-)
Perhaps there are issues with the file type on your machine and fs::file_info(path) returns something unexpected under some conditions.

Determining if there are unused packages in an R script [duplicate]

As my code evolves from version to version, I'm aware that there are some packages for which I've found better/more appropriate packages for the task at hand or whose purpose was limited to a section of code which I've now phased out.
Is there any easy way to tell which of the loaded packages are actually used in a given script? My header is beginning to get cluttered.
Update 2020-04-13
I've now updated the referenced function to use the abstract syntax tree (AST) instead of using regular expressions as before. This is a much more robust way of approaching the problem (it's still not completely ironclad). This is available from version 0.2.0 of funchir, now on CRAN.
I've just got around to writing a quick-and-dirty function to handle this which I call stale_package_check, and I've added it to my package (funchir).
e.g., if we save the following script as test.R:
library(data.table)
library(iotools)
DT = data.table(a = 1:3)
Then (from the directory with that script) run funchir::stale_package_check('test.R'), we'll get:
Functions matched from package data.table: data.table
**No exported functions matched from iotools**
Have you considered using packrat?
packrat::clean() would remove unused packages, for example.
I've written a command-line script to accomplish this task. You can find it in this Github gist. I'm sure there are edge cases that it misses, but it works pretty well, on both R scripts and Rmd files.
My approach always is to close my R script or IDE (i.e. RStudio) and then start it again.
After this I run my function without loading any dependecies/packages beforehand.
This should result in various warning and error messages telling you which functions couldn't be found and executed. This again will give you hints on what packages are necessary to load beforehand and which one you can leave out.

R, How do I use here() within parse()

I know how much you love the here() package... I'm just starting to catching on.
I am trying to write an (relatively) automated code for cleaning data and I would like to use here() and parse(). But parse() doesn't seem to like having here() within it.
cleaning <- parse(file = here("folder", "source-code.R"))
and I get the error:
Error in here("folder", "source-code.R"):
unused argument ("source-code.R")
If I set my working directory as the folder containing "source-code.R" and remove the here() argument, the process works just fine.
also, I've tried removing the "folder" and just calling the "source-code.R", but then I get the error:
Error in parse(file = here("source-code.R")) :
'file' must be a character string or connection
thanks for your help!
Thanks to MrFlick who recognized the naming conflict. Using here::here() solved it!

readxl (import excel file to R) error

When I typed the following line using readxl package, :
bluedata <- read_excel("data.xlsx", coffee)
I get the following error:
Error in standardise_sheet(sheet, range, sheets_fun(path)) :
object 'coffee' not found
What I want was to import the data in "coffee" sheet of the "data.xlsx" file.
Could any one help me to solve this problem?
Thanks!
It is cool that you are using readxl package, this is faster than normal way to read outside data.
I think you can find the answer yourself by typing
help("read.excel")
or
?"read.excel"
I can tell you the answer, but you will met thousands of another problem if you can not find answer on your own

What is read.data in R? Why can't I find it in any docs?

I'm trying to debug my first R script and I came across this line:
data <- read.data(dir, indiv, label)
I've been googling "R read.data" for the past 30 minutes and absolutely nothing is coming up. Am I doing something wrong? Is there a good way to look up things I see in R scripts that I don't know what they are?
And what is this particular line doing anyway?
It's probably a function defined by the author of the script. Search for it in the code you have.
A couple of things to check:
Does your script define the function 'read.data' somewhere? read.data <- function(...
Does your script use library() or require() to load another package? In that case, the read.data function can be defined in that package.
Does your script use source to read another script? Check that script then...
package sos to the rescue:
read.data is a deprecated function in the rjags package
> library(sos)
> findFn("read.data")
Finds this result:
http://finzi.psych.upenn.edu/R/library/rjags/html/read.data.html
From this page:
Read data for a JAGS model from a file.
Usage
read.jagsdata(file)
read.bugsdata(file)
Note
Earlier versions of the rjags package had a read.data function which read data
in either format, but the function name was ambiguous (There are many data file
format in R) so this is now deprecated.
There isn't a base function named read.data. If you want to find help for an R function (for example read.table), simply type ?read.table at the interactive prompt.
This line calls a read.data function which is either defined in that script or in something else it loads (such as libraries with the library() or require(), other scripts with source()). You'll need to search those sources to find this function.

Resources