Error: unexpected string constant in file path in R - r

I am trying to run this command in R in order to run a function:
xlsxToR <- function("C:\\Users\\Nabila\\Dropbox\\IsolutionsProject\\ServiceRequestTickets.zip", keep_sheets = "TicketDetails", header = FALSE)
However, I tend to get this error when I run it:
Error: unexpected string constant in "xlsxToR <- function("C:\\Users\\Nabila\\Dropbox\\IsolutionsProject\\ServiceRequestTickets.zip""
I have tried looking for a mistake in my file path. Tried using forward slashes but to no avail. Can anyone help please?

Functions in R
When you are using function in R you are defining a function (You can use ?function to see the documentation). Inside the parentheses after function you set the arguments of the function. You can also set default values for those arguments using =. After that the body of the function should follow, i.e. an R expression containing the code of the function.
Your case
This line of code does not run a function. It defines a function with name xlsxToR. The first thing in the parenthesis is a string, not an argument name, which causes the error. Also your function is just a definition of some argumnents without a body, which probably is not what you are trying to do.

Related

vectorizing the htm2txt function is throwing error: "Error in exists(y, hash, inherits = FALSE) : invalid first argument"

I am trying to convert a set of HTML emails to readable text. I am trying to use the htm2txt function from the package with the same name.
The problem is that the function is not vectorized and when I try to vectorize the function I get the error Error in exists(y, hash, inherits = FALSE) : invalid first argument
I have tried using the Vectorize() function to make a vectorized version of the function.
I have tried lapply. I have tried a plain for loop. But I keep getting the same error.

Can I use the output of a function in another R file?

I built a function that retrieves data from an Azure table via a REST API. I sourced the function, so I can reuse it in other R scripts.
The function is as below:
Connect_To_Azure_Table(Account, Container, Key)
and it returns as an output a table called Azure-table. The very last line of the code in the function is
head(Azure_table)
In my next script, I'm going to call that function and execute some data transformation.
However, while the function executes (and my Azure_table is previewed), I don't seem to be able to use it in the code to start performing my data transformation. For example, this is the beginning of my ETL function:
library(dplyr)
library(vroom)
library(tidyverse)
library(stringr)
#Connects to datasource
if(exists("Connect_To_Azure_Table", mode = "function")) {
source("ConnectToAzureTable.R")
}
Account <- "storageaccount"
Container <- "Usage"
Key <- "key"
Connect_To_Azure_Table(Account, Container, Key)
# Performs ETL process
colnames(Azure_table) <- gsub("value.", "", colnames(Azure_table)) # Removes prefix from column headers
Both the function and the table get warning. But while the function executes anyway, the Azure_table throws an error:
> # Performs ETL process
>
> colnames(Azure_table) <- gsub("value.", "", colnames(Azure_table)) # Removes prefix from column headers
Error in is.data.frame(x) : object 'Azure_table' not found
What should I be doing to use Azure_table in my script?
Thanks in advance!
~Alienvolm
You can ignore the RStudio warnings, they are based on a heuristic, and in this case it’s very imprecise and misleading.
However, there are some errors in your code.
Firstly, you’re only sourceing the code if the function was already defined. Surely you want to do it the other way round: source the code if the function does not yet exist. But furthermore that check is completely redundant: if you haven’t sourced the code which defines the function yet, the function won’t be defined. The existence check is unnecessary and misleading. Remove it:
source("ConnectToAzureTable.R")
Secondly, when you’re calling the function you’re not assigning its return value to any name. You probably meant to write the following:
Azure_table <- Connect_To_Azure_Table(Account, Container, Key)

Parsing error in MonteCarlo::MonteCarlo function in R

I am trying to run a power analysis using a MonteCarlo approach in R.
I have created a function of two parameters that does output a boolean (tested manually for all relevant values of the parameters). I also have run baby-examples of the MonteCarlo function to make sure that I understand it and that it works well.
Yet when I try to run the real thing, I get the following error message:
Error in parse(text = all_funcs_found[i]) : <text>:1:1: unexpected '::'
1: ::
I read through the source code of the MonteCarlo function (which I found here) and found
#loop through non-primitive functions used in func and check from which package they are
for(i in 1:length(all_funcs_found)){
if(environmentName(environment(eval(parse(text=all_funcs_found[i]))))%in%env_names){
packages<-c(packages,env_names[which(env_names==environmentName(environment(eval(parse(text=all_funcs_found[i])))))])
}
}
which doesn't really make sense to me - why should there be a problem there?
Thank you for any ideas.
I found the answer: the function I wrote was calling a function from a specific library in the form libraryname::functionname.
This works OK if you use the function once manually, but makes MonteCarlo break.
I solved the problem by first loading the relevant library, then removing the 'libraryname::' part from the definition of the main function. MonteCarlo then runs just fine.

Extract argument code of a function in R/Subset function based on argument names

I am working with caret package. There are many functions which uses methods inside it. Example,
rfe
methods(rfe)
rfe.default
To check arguments of rfe.default we use args() function, or formalArgs() or formals()
args(rfe.default)
formalArgs(rfe.default)
formals(rfe.default)
Now, I just want to see the codes for each argument used inside rfe.default function, instead of whole function code which we get by typing
rfe.default
How to get the codes just for those arguments.
We can use deparse() function to get the range of lines from the function code. But is there any way to get the codes of arguments from the function based on argument names.
If an argument is using a function within it like here rfeContol = rfeControl()
I should be able to extract just this argument's code from rfe.default function instead of the whole code or rfe.default.
Thanks.

Searching a functions source code

In R, you can view the source of a function as a function is simply another object.
I am looking for a way to search through this source code, without knowing the file that the source is saved in.
For example, I might want to know if the function shapiro.test contains the function sort (it does).
If shapiro.test was a string or a vector of strings I would use
grep('sort', shapiro.test)
But as shapiro.test is a function, this gives the error "Error in as.character(x) :
cannot coerce type 'closure' to vector of type 'character'".
I've had no luck trying to coerce the function to a string. Just as an extra, I'm not expecting to be able to search through base functions as they are compiled.
Here a solution using deparse:
> grep ("sort", deparse(shapiro.test))
[1] 5
You could wrap the function in capture.output, which will convert each line to an element in a character vector.
> grep("sort",capture.output(shapiro.test))
[1] 5
Or you could just call edit(shapiro.test) and use the text editor specified by options(editor=) to search through the function.

Resources