Why can't I change the working directory in RStudio? - r

I got some example codes from a course that start with
install.packages('maps')
install.packages('geosphere')
library(maps)
library(geosphere)
setwd("C:\\mydata")
...
Everything was fine until the command of setting working directory. Because I installed RStudio and R in D drive, I have also tried "C://", "C:/", "C:\", "D:\", "D:\", "D://", and "D:/". However, the console kept saying "Error in setwd( ): cannot change working directory".
This is probably a studpid question. But I'm a beginner in R, so could somebody be so kind to help me out? Thank you very much!

Related

After loading R script, it returns lots of dot

I have been working on an R script for the last several weeks. When I opened this R script this morning, I found nothing but a list of dots showed in the attached figure. I tried both RStudio and R i386, and returned the same result. I also asked my colleague to open it, but he also failed even open it in the text file. The .Rhistory can not be well opened either. Could someone help me out? You can find the R script in the shared link below. Many thanks.
https://drive.google.com/file/d/1w3wbArOXjRoOXJlrrDvHxFDjwqNobjcy/view?usp=sharing

compareGroups package - how to specify destination path in R when exporting files?

Beginner to R! Bit of a bizarre question, but is anyone familiar with the compareGroups package in R? There's a function to export files, but I have no idea where the file ends up in - can't seem to find it doing a computer search. (I use RStudio). The CRAN guide wasn't particularly helpful.
The functions I tried were export2csv() and export2word()
Thank you!
Found a solution. Oddly the formula doesn't follow instructions in the package's manual. In case anyone else gets the same problem, export2word(createTable(compareGroups(x ~ y1 + y2, data=dataset), file="filename.doc")
If you run
getwd()
you should get back the directory path that RStudio is using for your current project at that moment. If you do not specify a special destination for the export, I believe it should output it in that directory. You can also run
list.files()
to list the files in your current working project directory

I pointed the file.choose() command to a file titled "mytest.r". How do exit from this now?

I am a novice trying to learn R using the swirl library. I entered the below command after which the screen just lets me type endlessly.
file.choose("mytest2.r")
Could someone help me understand what exactly is expected here and how to exit out of this? mytest2.r is just a blank file.

Run Rmarkdown.rmd outside R editor

I am still pretty new to R, but couldn't seem to find solution to my question.
It's quite simple:
I have a .rmd file written and ready to run, but instead of openning it from R or Rstudio, Is there a way I can knit it automatically without opening that file?
Please kindly guide me how should I do this, screenshot would be much appreciated!!
Thanks in advance!!
Added: So I took nrussell's advice and tried run that on my windows command line, but I am seeing "The filename, directory name, or volumn label syntax is incorrect". Thoughts? Thank you all for your comments!
did a cd RMD's path , then used #nrussell's code. and it worked.
Somehow, I couldn't use specified RMD file path such as C:\XXX\XXXX.rmd, but if I cd to there first, it worked.

Error opening SHP file in R using maptools readShapePoly

I am new to R and was following the following tutorial on the ggplot2 package found here. However the readShapePoly() function throws an error whenever I try to load the basic shapefile. I have used the following code:
library("ggplot2")
library("gpclib")
library("maptools")
setwd("~/Documents/R Projects/Intro to ggplot2")
#Intro to ggplot 2 contains the .shp file and associated data
sport <- readShapePoly("london_sport.shp")
which gets me:
Error in getinfo.shape(filen) : Error opening SHP file
I have tried omitting the file extension. I have also tried downloading other .shp files which throw the same error too. I have also tried calling readShapePoly using the full file path, which doesn't work either. I am using R studio (mac OSX), but I get the same error using the standard R window. I have tried the suggestions on the previous closed threat "Error opening SHP file in Rstudio", but to no avail.
Edit: the error was with a missing .dbf file. Thanks to #Spacedman for the fix.
I had a similar issue, and it was because there were several other files along with the '*.shp' shape file in the zip package that I downloaded. Then I only moved the shape file to another folder and it didn't work. When including all files together, it was fine and I could readShapeSpatial() function okay.
Forget ggplot and gpcclib. Stick to maptools and rgdal that actually provide tools for reading a shapefile.
Don't just say you've tried "this and that", outline the details. For example, does `file.exists("london_sport.shp") return TRUE?
Also, what makes you think readShapePoly() is the right function? It only knows how to read polygon shapefiles, try readShapePoints() and readShapeLines() as well.
If you can, try readOGR which can read a shapefile despite many caveats (including the geometry type).
library(rgdal)
readOGR("~/Documents/R Projects/Intro to ggplot2", "london_sport")
If you can report on all of those things it's likely someone could help.
There is one more, but not much automatic solution which helped me:
file<- readShapePoly(file.choose())
Then just find your *.shp file and run it.
Include three of those files (extensions: dbf, shp, shx) in the same folder.
In case this helps anyone:
I had the same problem but none of the solutions worked. Worst, I the same was going on with an script that I'm 100% sure was working previously.
Turns out that it could also be that the shapefile gets damaged. Apparently this can happen while the file is being manipulated (or so that say my geographer friends), so next time you try to open it won't work for no apparent reason. Downloading it again worked fine, but makes me think to have a copy of the more precious ones just in case.
I just managed to fix this problem with a shapefile I was trying to read by typing:
file<- readShapeSpatial("filename.shp")
instead of typing in the full file path.
Before then, I tried all the suggestions, including making sure that the .dbf and .shx files were also present. Don't know why this should be the case.
I had the same problem. I found out that basically you need three of those files: .shp, .shx and .dbf

Resources