How can I upload a genepop file? Keep getting error message - r

I am trying to analyze microsatellite data and am trying to use the Genepop program.
I made a txt document with my data but when I try to run the program I get this message:
There is no such file in the package 'extdata' directory
I have the working directory set to the right file.

Related

Downloading and unzipping GitHub zipped files directly in R

I am trying to download and unzip a folder of files from GitHub into R. I can manually download the file at https://github.com/dylangomes/SO/blob/main/Shape.zip and then extract all files in working directory, but I'd like to work directly from R.
utils::unzip("https://github.com/dylangomes/SO/blob/main/Shape.zip")
# Warning message:
# In utils::unzip("https://github.com/dylangomes/SO/blob/main/Shape.zip", :
# error 1 in extracting from zip file
It says it is a warning message, although nothing has been downloaded or unzipped into my wd.
I can download the file to my machine:
utils::download.file("https://github.com/dylangomes/SO/blob/main/Shape.zip")
But I get the same message with the unzip function:
utils::unzip("Shape.zip")
And the downloaded file cannot manually be extracted. Here, I get the error that the compressed folder is empty. The unzip line works on the manually downloaded .zip file, which tells me something is wrong with the download.file line.
So if I add raw=TRUE to the end (which can make a difference in downloading data from GitHub):
utils::download.file("https://github.com/dylangomes/SO/blob/main/Shape.zip?raw=TRUE","Shape.zip")
utils::unzip("Shape.zip")
I get a different warning with, similarly, nothing being executed:
Warning message:
In utils::unzip("Shape.zip") : internal error in 'unz' code
I have tried most of the answers at Using R to download zipped data file, extract, and import data, but they appear to be for single files that are zipped and aren't helping here. I've tried the answers at r function unzip error 1 in extracting from zip file, which mentions the same warning message I am getting, but none of the solutions work in this case.
Any idea of what I am doing wrong?
You need to use:
download.file(
"https://github.com/dylangomes/SO/blob/main/Shape.zip?raw=TRUE",
"Shape.zip",
mode = "wb"
)
Without the query string ?raw=TRUE you are downloading the webpage and not the file.
(For Windows) R will use mode = "wb" by default when it detects from the end of the URL that certain file formats, including .zip, are being downloaded. However, the URL finishing with a query string instead of a file format means the check fails so you need to set the mode explicitly.

Set wd in RStudio

I am creating a series of r scripts that will be used by multiple people, meaning that the working directory of files used and stored will differ. There are two folders, one for the R code, called "rcode," and another to store the generated outputs, called "data". These two folders will always be shared in tandem. To accommodate for the changing working directory I created a "global" script that has the following lines of code and resides in the "rcode" folder:
source_path = rstudioapi::getActiveDocumentContext()$path
setwd(dirname(source_path))
swd_data <- paste0("..\\data\\")
The first line gets the source path of the global script. The second line makes this the working directory. The third line essentially tells the script to store an output in the "data" folder, which has the same path as the "rcode" folder. So to read in a csv file within the "data" folder I write:
old_total_demand <- read.csv(paste0(swd_data, "boerne_total_demand.csv"))
When I use this script on my Windows laptop it works beautifully, but when I use it on my Mac I get the following error:
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
cannot open file '..\data\demand\boerne_total_demand.csv': No such file or directory
Would anyone have any idea why this would be? Thanks in advance for the help.
I'm not sure what systems your collaborators will be using, but you may run into issues due to differences between Window/Mac/Linux with regards to how paths are written. I suggest you create a R Project .Rprj using RStudio and save that in your directory that contains subdirectories for data and rcode, and share the entire project directory.
/Project_dir/MyProject.Rprj
/Project_dir/data/
/Project_dir/rcode/
Then from the R project opened through RStudio you should be able to directly refer to your data by:
data <- read.csv("data/boerne_total_demand.csv")
The working directory will always be where your .Rproj is stored, so you can avoid having to setwd as it causes lots of chaos when sharing and collaborating with others.
I have this code from my current script at hand.
I hope you like it !
path <- dirname(getActiveDocumentContext()$path)
setwd(path)
swd_path <- paste0(path,"/data/")
if(!dir.exists(swd_path)){
dir.create(swd_path)
}
old_total_demand <- read.csv(paste0(swd_data, "boerne_total_demand.csv"))

Why is the working directory overwritten to the directory of the current Rmd file?

I have an R.proj file called Food_Choices.Rproj that is supposed to be setting my working directory to ~/Desktop/Food_Choices, a folder containing reproducibility files according to the TIER system like
But it's not setting the working directory properly, because when I knit my processing file with code like this
food<-read_csv("Original_Data/food_coded.csv")
#imagine some processing code in between here
write.csv(food, file = "Analysis-Data/analysis_data.csv")
I get this error:
Error: 'Original_Data/food_coded.csv' does not exist in current working directory ('/Users/IdanCarre/Desktop/Food_Choices/Command_Files').
Which is not the project directory, it's the directory of the processing file!
I thought I set the working directory when I opened the files in the context of the R project, but that doesn't seem to be happening anymore (even though my files from a year ago with the same setup still work??)
NOTE: I don't want to use
library(knitr)
opts_knit$set(root.dir = '/Users/IdanCarre/Desktop/Food_Choices')
Because then new users who want to reproduce the results have to go manually insert their own directory into each file they want to run. That's a lot of work they shouldn't have to do.
UPDATE TO COMMENTS:
I used the here package, and that works satisfactorily for read.csv (it throws a data column de-duplication warning but I think it's probably okay for now), but when I write out the processed data file to the analysis data folder, I'm trying to use
write.csv(food, file = here("Analysis-Data", "analysis_data.csv"))
And the error I get is
Error in file(file, ifelse(append, "a", "w")) : cannot open the connection
I get this same problem if I use
write.csv(food, file = "Analysis-Data/analysis_data.csv")

Cannot load my CSV file into my R? keep getting error messages

So basically I succesfully exported my SQL view data into a csv file. but no when I load into Rgui software, I get the following errror:
> load("C:\\Users\\dachen\\Documents\\vTargetBuyers.csv")
Error: bad restore file magic number (file may be corrupted) -- no data loaded
In addition: Warning message:
file ‘vTargetBuyers.csv’ has magic number 'Marit'
Use of save versions prior to 2 is deprecated
What should I do? Is it the R version installed wrong? or something wrong with my CSV file?
Try using read.csv instead of load. load is for reading files created by save.
Type ?read.csv to access the documentation.

Intermittant "Permission Denied" error when compiling Latex document with Knitr and openxlsx

I am using a data set that has been provided in an xlsx workbook across several worksheets. I created a function to extract the data from one of the sheets and reformat it into a usable form.
Opening the file twice in one compilation of Latex often (but not all the time) triggers the following error:
Error in unzip(xlsxFile, exdir = xmlDir) :
cannot open file 'C:/Users/Jonno/AppData/Local/Temp/RtmpewoYVY/_excelXMLRead/xl/worksheets/sheet5.xml': Permission denied
This suggests that the temporary file is not being closed which prevents openxlsx from unzipping the xlsx file again.
Unfortunately the file needs to be opened several times to extract the data.
Also unfortunately I haven't been able to create a reproducible example suggests that it may be something to do with the structure of the file I have received.
Is there a way to force the temporary file to be closed and deleted after each time it is opened?
This problem only occurs when knitting, when it is run in the R enivironment this problem never occurs.

Resources