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

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!

Related

Can import dataset into RStudio but can't read file

I can see the table of the data set on the upper right of Data.
My code is currently:
file.exists(C:/Hollywood.xlsx)?
to see if RStudio is picking up on it.
I get an error message of
"Incomplete expression: file.exists(C:Hollywood.xlsx)?"
To take a look at the working directory, I'm doing Session>Set Working Directory>Source File Location and get "The currently active source file is not saved so doesn't have a directory to change into." If I instead try Session>Set Working Directory>To Files Pane Location, I get setwd(~) and the following is what I get and subsequent code I try:
setwd("/Users/FridasSlave/Downloads/Hollywood")
#Error in setwd("/Users/FridasSlave/Downloads/Hollywood") :
# cannot change working directory
getwd(Hollywood)
# Error in getwd(Hollywood) : unused argument (Hollywood)
getwd("Hollywood")
#Error in getwd("Hollywood") : unused argument ("Hollywood")
If I try Session>Set Working Directory>Choose Directory, I can get to my Downloads folder but the individual files are all not there.
What am I doing wrong?
(And yes, I'm very new to R.)
I have all my relevant paths and code above.
Scrolling down the list of problems:
I hope you didn't put a ? at the end of an R command. It's true that it looks like it would be a question if you were talking to a person, but that's not the case.
The argument to file.exists should be a character value. C:/Hollywood.xlsx is not recognized by the R interpreter as a character value because there are no quotes around it. It's possible that at that point you could have gotten success with
file.exists("Hollywood.xlsx") # if it is in the current working directory
From your use of "~" and setwd("/Users/FridasSlave/Downloads/Hollywood") I'm guessing that you are on a Mac. Your question should probably have included that as a specific note. (Which further raises the issue why you would use a Windows file spcification if you are on a Mac?
The reason for the error from setwd("/Users/FridasSlave/Downloads/Hollywood") is not obvious, but maybe you misspelled part of the path or maybe it should have been setwd("/Users/FridasSlave/Downloads/").
Trying getwd(Hollywood) you again failed to include quotes around the argument. And why would you even want an argument if you want information about the current working directory? Should have just been getwd()

R error message for unused argument (writexl)

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?

Don't print the name of the directory to stdout when using here::set_here('path')

I'm using the R package 'here' to define my working directory using the following command at the start of my script:
here::set_here(path='/path/to/my_directory/', verbose = F)
Every time I run the script it prints this to the console:
here() starts at /path/to/my_directory
Is there a way to suppress this output? I tried using the invisible() function but that didn't work...
The message you’re seeing is printed when you’re attaching the ‹here› package. Simply don’t do that (it’s unnecessary anyway) to prevent it.
Otherwise, load it as follows:
suppressPackageStartupMessages(library(here))
… yeah, not exactly elegant.

Error with setwd in R

When trying to use the sample code for SubgraphMining (the example is on 35th page), I get an error:
"Error in setwd(paste(Sys.getenv("R_HOME"), "library", "subgraphMining", :
cannot change working directory"
I'm using RStudio 0.97.551, 32-bit R (2.15.3 - this version of R was recommended to use with subgraphMining), igraph0 (was recommeded too, instead of igraph library), Java installed. Operation system is Windows 8.
Can anyone help me with the issue?
The error message is coming from the gspan function of subgraphMining, from here:
setwd(paste(Sys.getenv("R_HOME"), "library", "subgraphMining",
"parsemis", sep = "\\"))
The reason for it is that R uses / as path separator, and not \\, which only works on windows. A workaround is not modify the function and use / instead of \\.
Btw. this has nothing to do with the igraph package, so I'll remove that tag.
In my case, it displayed the error because I expected it to create a new folder which I mentioned in the path in setwd. Unfortunately, R does not have this functionality and the matter was resolved when I created the folder and then used setwd command.
I know it's almost 1 year since this question was posted. I encountered the same problem with subgraphMining package. A quick hack is: You can write "gspan" on RStudio's command line and it will show the function, copy that function and create your own function in your own script (of course with new name, let's say gspanNew) and fix it by replacing "\\" with "/", as Gabor Csardi pointed out.
Cheers! :)
You can always use file.path("path","with","code") instead of simple paste in order for your code to be OS independent.

Unable to find function in workspace

I used lapply to run a function 100 times with below argument:
lapply(1:100, myfunc)
but I got this error message:
error in match.fun(FUN) : object 'myfunc' not found
I used list.files() to check that if function is in working directory and it was there. So I don't know why I got this message.
This sounds vaguely like you're expecting R to behave like Matlab (based on my hazy memories of Matlab).
Just because a file containing a function is in your working directory does not mean anything about whether R knows about it. You need to source() the file containing the function, and then verify that the function exists in your workspace using ls().
So for example, if the file containing the function is in "foo/bar/myfile.R" then I'd run:
source("foo/bar/myfile.R")
and the verify that the function is in my workspace by running ls().

Resources