Error when trying to copy xlsx file path into R - r

I am trying to import a xlsx file into R but I keep getting this error when copying the file path. The file path is "C:\Users\aj\Downloads" so I have been writing:
library(openxlsx)
Data <- read.xlsx(paste0(C:\Users\aj\Downloads, '\Data.xlsx'))
I tried switching to "/" from "" and I got a different error that stated unexpected input. Does anybody know what I am missing or if I am even on the right track?

As a work-around: you could use the Import Dataset button in the Environment pane. It will import the dataset for you and also show you the correct command.

Related

How to use grep with fread in Rstudio?

I have a large csv file that I want to import into Rstudio. Because it is so big (~29GB), I would like to only import specific rows at a time, rather than import the whole thing and then filter. I have been trying to use grep in the cmd = argument in fread(), however, I keep getting the message that
'grep' is not a recognized internal or external command, operable program, or batch file.
I have installed http://cygwin.com, and have even tried manually adding the path (C:\cygwin64\bin) to the Windows environment, but still, I keep getting the error message.
As such, I suppose I have two questions:
How do I get grep to work when using fread to import a CSV into Rstudio?
Once grep is working, what syntax would I use to only import rows that contain a certain string?
library(rgbif)
library(data.table)
#Download the csv (note: downloads a zip file ~7GB, unzipped ~29GB
occ_download_get("0299151-200613084148143") #add argument, path = ,if you want to direct the download
#I now only want to import the rows that contain "Acanthiza pusilla", for example.
fread() # it's here that I am unsure of the correct syntax
Any help would be appreciated. Additionally, let me know if you require more information.
Thanks
fread(cmd='findstr /r Acanthiza.pusilla [your file location]"')
ex) fread(cmd='findstr /r Acanthiza.pusilla \"C:\\Users\\abc\\Documents\\test.csv\"')

How do I import data via code in R (Instead of using the import in the menu bar) from code typed into an R notebook?

Every time I type in the file name in this case "labelled edited.xlsx" (perfectly - it was copied from the import box when using the import function from the menu into an R notebook), then try to run it, it says 'Error: path does not exist'. However using the import menu works. If I copy and paste the exact same thing from the import box:
labellededited <- read_excel("labelled edited.xlsx", col_names = TRUE, .name_repair="minimal")
into the notebook and run it immediately, it works perfectly. However, when I close R, open it again, set the working directory (without changing a single thing in the directory folder so the file names are the same), it returns the error even though absolutely nothing has changed - I just restarted R.
In addition to this, copying the code from the notebook into the import box on the bottom right will import the dataset perfectly, as does copying the line of code into the console. It only happens when I press cmd+enter directly from the notebook.
Any tips on fixing this? I know it's not a big deal, but ideally, i'd like to create a code, set the directory and then just let it run.
The problem has to do with RStudio and file types. In order to use the keyboard shortcuts (Ctrl+Enter) the commands have to be saved as an R script file. So start a new one (Ctrl+Shift+N) and copy the commands from the .Rmd file, and try again.
Hi you can use this i guess,
set working directory using setwd("your Path/") then
library(readxl)
if you want to import xlsx use read_xlsx , if you want to import xls use read_xls
labellededited <- read_xlsx("labelled edited.xlsx",sheet = "select sheet number"(default it will consider as first sheet)
more better way you can keep path inside the code and import the file(if you don't move the file it will import without any error)
labellededited <- read_xlsx("yourpath/labelled edited.xlsx",sheet = "select sheet number")
Hope it helps

Unable to read SPSS file from working directory

I have created a folder that has my dataset and set it as my working directory in Rstudio.
The dataset is an SPSS file which I named "Stats in R", I downloaded the packages foreign and Hmisc and tried to run to the following command to get the dataset:
data = read.csv("Stats in R.csv", header = TRUE)
…but the console showed the following message:
Error in file.choose() : file choice cancelled.
The problem is, my dataset is in the working directory but whenever I try to open it, R shows me an empty folder. I run the dir() command and instead of getting the directory content in the console, I get the following message:
character(0).
I really don't know what's the problem, the SPSS dataset is saved as a SAV file. I tried other extensions as well like CSV but nothing worked. Any suggestions, please? I really need to sort out that issue soon. Thank you!
It seems like you have some issue with your working directory - it's either not where you think it is, or you don't have full permissions and something is blocking the R Studio interface from accessing it.
Since you want to get this resolved quickly, the approach I would try is importing the file using the entire file path instead of the working directory, and then you can troubleshoot your WD when you have time.
This should work:
df <- read.csv("C://Users//Mina//Folder//Subfolder//Stats in R.csv", header = TRUE)

Read excel file cannot find the path?

library("readxl")
my_data <- read_excel("GVA.xlsx")
my_data
however the console says the path does not exist.
How can I import excel/csv files and know that the file will always be found.
Why does this not work?
p.s.
I am new to R
Well, is 'GVA.xlsx' in your working directory? If not, R can't find it because you're not mapping to anything outside of your current directory. You can navigate to the file by clicking on: File > Import Dataset > From Excel. Browse to your file and set any required import options. R will actually create the code to map to the file in question.

How to code data files from excel into Rstudio

I am trying to import data from excel into R (stats class), but we are not allowed to use the import function. We have to code it in. I got help last week from a graduate student, but I think because I got a new laptop over the weekend, the Rmd file cannot be used as my reference markdown anymore. Here is what I've tried inputting:
library(tidyverse)
data.frame(readxl::read_excel(path = "./data/thedataweprovide.xlsx", sheet = "Problem 1"))
Error: `path` does not exist: ‘./data/thedataweprovide.xlsx’
> setwd("C:/Users/Jasmine Ferrell/Documents/BME_Statistical_Methods/Project_1")
Error in setwd("C:/Users/Jasmine Ferrell/Documents/BME_Statistical_Methods/Project_1") :
cannot change working directory
> pone
Error: object 'pone' not found
> setwd("C:/Users/Jasmine Ferrell/Documents/BME_Statistical_Methods/Project_1")
Error in setwd("C:/Users/Jasmine Ferrell/Documents/BME_Statistical_Methods/Project_1") :
cannot change working directory
> setwd("C:\Users\ferre\OneDrive\Documents\BME_Statistical_Methods\Project_1")
Error: '\U' used without hex digits in character string starting ""C:\U"
> setwd(C:/Users/ferre/OneDrive\Documents/BME_Statistical_Methods/Project_1)
Error: unexpected '/' in "setwd(C:/"
It looks like your paths are the problem. You could first try to get the working directory you are currently in with the function getwd() this may help you to figure out how you could correct your paths. Do not use single Backslashes (\) in paths, only use foreward-slashes (/).
As saae mentioned its a problem with path. Try these and should be able to get it working.
Type in your Rstudio
getwd()
This gives the current path which is currently set. If you are files are somewhere else, then you won't find it.
To change the path
go to your file location and copy the path. For example in windows, you go to the folder and click on properties, there you will see location. Copy the location and add it to the below code.
setwd("//abcdefg/somerandomguy/Profile/Desktop/My files/R_project")
Then you can run your code to read the files.

Resources