I tried to display a folder using R script. I know commands like file.choose() and file.show() but I would like to run an R script to display the folder (a real folder, not the picture nor a screenshot of it). After display the folder, the script is done. I'd like to be able to choose to open any file in the folder but won't import anything into R.
file.show() will display a exactly file but I would like to display a folder. Maybe I can say what I am looking for is folder.show() or dictionary.show()??
file.choose() will import that file to R but I do not want to import anything into R
Any ideas?
Try ( if OS=windows)
shell("explorer C:\\Users\\xxx\\yy", intern=TRUE)
Related
Is it possible to show a file directory using function/code? I find myself clicking through file structures to view a directory. I'd like to use some like this:
my_search_finding <- grep('search-pattern', list.files('~/some/long/directory/tree'))
new_function_to_view_files(my_search_finding)
Then the directory is opened in that viewer display and I can explore using the mouse.
Thanks
Under Windows, you can use choose.files:
path <- '~/some/long/directory/tree'
selected.files <- choose.files(default=paste0(path, "/*.*"))
the default argument allows you to display files in a particular directory with a fully qualified file mask, see documentation.
I want to know how can I import a .txt file in R, but avoiding pathing to my file. I usually import like this: "Import Dataset" and the a select "From text(base)", but when I write in the program file.exists("myfilename.txt") it tells me FALSE. How can I do it correctly?
When you run file.exists("myfilename.txt"), R will search your current working directory for a file called myfilename.txt. If you have a file called myfilename.txt that you imported from some other directory, then file.exists("myfilename.txt") will return FALSE.
Solution 1:
Put your R script and the myfilename.txt file in the same folder
Change your working directory to that folder, either using the session menu or using setwd("path/to/folder")
file.exists("myfilename.txt") should now return TRUE
You can read your table with read.delim("myfilename.txt")
Solution 2:
Create an Rstudio project
Place your R script and the myfilename.txt file in the project folder.
Every time you open the project, your working directory will point to the project folder.
file.exists("myfilename.txt") is TRUE
You can read your table using read.delim("myfilename.txt").
Solution 3:
Leave myfilename.txt where it is and read it by providing the absolute path, for example: read.delim("C:/Users/Jiakai/Documents/myfilename.txt")
In this case file.exists("myfilename.txt") is FALSE and file.exists("C:/Users/Jiakai/Documents/myfilename.txt") is TRUE.
If you want file.exists("myfilename.txt") to return TRUE change your working directory to "C:/Users/Jiakai/Documents/myfilename.txt".
To import a txt file, you have several options. The two best options are
readr::read_delim("path/tomyfile/myfilename.txt", delim = "\t")
or
data.table::fread("path/tomyfile/myfilename.txt", sep = "\t")
They are preferrable to the base R read.delim function that is slower.
You can provide absolute paths or relative path, if you know your working directory
Edit
If you don't know your working directory, you can run
getwd()
If you don't find your file with file.exists it means you need to change your working directory or change the path in your import and file.exists command
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
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.
I'm making a simple line in r to automatically open my generated plots.
I output the plots to a file called "plots.pdf" in the same directory as my r file, and at the end i use this two lines to try to open it:
dir <- paste("/Applications/Skim.app/Contents/MacOS/Skim ",getwd(),"/plots.pdf",sep="")
system(dir)
Basically, dir concatenates the full path of the skim app and the full path of the generated plot.
If i run the string stored at dir in a shell it works perfect, it opens the pdf file in Skim, but when i run it with system() from inside R it doesn't work (Skim says 'The document “plots.pdf” could not be opened.').
I believe this is a very little mistake somewhere in the syntax regarding the absolute/relative paths, but haven't managed to find it... Any advice is welcome! (Or a better way to achieve the same)
I found a way to bypass that problem, i just changed the path to Skim for the 'open' command and i let the system to assign the default app for pdf viewing. So:
dir <- paste("open ",getwd(),"/plots.pdf",sep="")
And it works.