7

I am trying to link up my excel data set to R for statistical analysis. I am running on OSX Sierra (10.12.6) with R studio (1.0.153) and Java 8 (update 144).

The function "read_excel" was able to open my excel document a week ago. When I moved the excel and the R document together to another folder, it no longer worked. Reloading the libraries has had no effect. After multiple attempts (and restarting R studio and computer), something finally worked but function "lmer" was no longer found. After reloading library "lme4", "read_excel" no longer worked!

I have also tried using "read.xlsx" and "readWorksheet(loadWorkbook(...))", which didn't work. "read.csv" also did not work properly since the commas were creating disorganized columns and I am dealing with a larger excel workbook with ongoing changes.

Reading on Stack, question Importing .xlsx file into R has not resolved my issue! Please help!

Libraries loaded:

library(multcomp)
library(nlme)
library(XLConnect)
library(XLConnectJars)
library(lme4)
library(car)
library(rJava)
library(xlsx)
library(readxl)

R data file:

Dataset <- read_excel("Example.xlsx",sheet="testing")
#alternative line: Dataset <- read.xlsx("~/Desktop/My Stuff/Sample/Example.xlsx", sheet=7)

Dataset$AAA <- as.factor(Dataset$AAA)
Dataset$BBB <- as.factor(Dataset$BBB)
Dataset$CCC <- as.numeric(Dataset$CCC)
Dataset$DDD <- as.numeric(Dataset$DDD)

Dataset_lme = lmer(CCC ~ AAA + BBB + (1|DDD), data=Dataset)
aosmith
  • 34,856
  • 9
  • 84
  • 118
user8760612
  • 73
  • 1
  • 1
  • 4
  • 2
    What are the exact error messages you are seeing and where exactly do they occur? Try running your script one line at a time to verify. – MrFlick Oct 11 '17 at 18:14
  • > at this point it doesn't run past the first line "Dataset <- read_excel("Example.xlsx", sheet = "testing") Error in read_excel("Example.xlsx", sheet = "testing") : could not find function "read_excel"" – user8760612 Oct 11 '17 at 20:14
  • Well, if you ran `library(readxl)` prior to that, that seems very odd. Did you get an error when running `library(readxl)`? After you run it does "package:readxl" show up in `search()`? – MrFlick Oct 11 '17 at 20:16
  • after I installed the library it shows that it's downloaded, but when I use the search() function, none of my loaded libraries show up. This is what I get: [1] ".GlobalEnv" "tools:rstudio" "package:stats" "package:graphics" [5] "package:grDevices" "package:utils" "package:datasets" "package:methods" [9] "Autoloads" "package:base" – user8760612 Oct 11 '17 at 20:29
  • Well, installing isn't the same as loading. You only install once, but you need to load at the beginning of each R session. Did you actually run `library(readxl)` in the console to load the installed package? You have that command listed above so I assumed you ran it. – MrFlick Oct 11 '17 at 20:34

3 Answers3

8

While you called the library, try and see if adding readxl::read_excel(path = "yourPath",sheet=1), or even remove the sheet reference. It will automatically take the first sheet.

carl
  • 96
  • 2
  • 1
    Thanks Carl! This allowed the script to run past the first line! What you've added, is this just a way to specify what library to look for the function? It still doesn't run past the "lmer" function in the script (error = not finding the function). – user8760612 Oct 11 '17 at 20:19
3

Perhaps, when you moved the excel and R file to another folder, the pathway should be change either. Try change the pathway, or replace the pathay by file.choose() and search the excel file manually. You called the package "xlsx", which can do the thing what you need. Maybe you're typing it wrong.

Dataset <- read.xlsx("Example.xlsx",sheetName="testing")

or

Dataset <- read.xlsx("Example.xlsx",sheetIndex="number of the excel sheet")

I hope it helps.

Luis Antolin
  • 144
  • 1
  • 7
1

Try activating library(tidyverse) and library(readr) then use the read_excel().This should work.

Abhijit Pai
  • 49
  • 1
  • 3
  • From Review: Hi, this post does not seem to provide a [quality answer](https://stackoverflow.com/help/how-to-answer) to the question. Please either edit your answer and improve it, or just post it as a comment to the question/other answer. – sɐunıɔןɐqɐp Apr 23 '20 at 06:45