I have a shortcut to a file (or a folder) under Windows, and I would like to resolve the path name under R and use that info to open a file.
The shortcut is called shortcut.lnk and it is placed in my working directory, and the shortcut is directed to another place, say C:\Users\XX\Desktop\something.txt
I would like to extract the path name of the shortcut to use that info to open the file, something like: read.table(resolved.link). I tried Sys.readlink, but it does not work on Windows.
This could help you :
library(R.utils)
lnk <- readWindowsShortcut("C:/Users/indi/Desktop/a.lnk")
lnk$pathname
More about it here
Related
I create a text file below:
myFile<-file("toGoogle.txt")
writeLines(c("InternetShortcut]","URL=http://www.google.com","IconFile=http://www.google.com/favicon.ico",
"IconIndex=0"), myFile)
close(myFile)
For clarity the text file now contains the following:
[InternetShortcut]
URL=http://www.google.com
IconFile=http://www.google.com/favicon.ico
IconIndex=0
In Windows, if I manually rename the file to "toGoogle.url" I am prompted:
After saying "Yes" the file is now a usable web link:
However when I use file.rename or any other substitute in R to change the file extension of toGoogle.txt it creates a dead web link with a target of "".
Is there a simple way out there to create a web shortcut using R? Or how can I change the file extension in R in a way that Windows is going to recognize it?
Edit: Using R to make the url weblink gives this:
And opening it prompts me with this:
The function file.rename works for me. And why not use file("toGoogle.url") to create a .url file directly.
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 have a simple script file called test.R. It's saved in the working directory. When I go to File > Open > test.R, it opens the file in the editor (without executing the code, which is what I want).
How do I do this by typing a command in the console window? To be clear, I just want to open (not execute) the file.
You are looking for file.edit
file.edit('test.R')
should open the file in an editor (if you are in RStudio, this should default to RStudio)
It appears that if you are using RStudio then another option is to use rstudioapi::navigateToFile("test.R")
I had the same issue that utils::file_edit() within my package opens outside R-studio. I changed it to usethis::edit_file(). With this function the file opens inside R-studio.
Another alternative is the shell.exec function.
shell.exec("text.R") # This will open the file or URL using the path associated with it
Also, I think for your use case. This code snippet might be a little bit useful.
file <- "example.csv"
sub_dir <- "subdirectory"
dir.create(sub_dir)
writeLines("myfile",file.path(sub_dir, file))
# Works
shell.exec(file.path(sub_dir, file, fsep = "\\"))
shell.exec(file.path(sub_dir, file))
When I do this in RStudio or RGUI:
fix(SomeFunction)
(or using edit()) I can see the function's code in Notepad. Is there any way that I could change this so that the code preview opens in Notepad++ rather than plain old Notepad? And similarly, is there anyway that I could force View(SomeDataFrame) to open up in Excel?
fix and edit functions invoke the editor defined in the "editor" argument.
By default, that argument is set to getOption('editor') as shown in the edit function documentation.
Therefore, you can either pass the notepad++ path as function argument, i.e. :
path <- "C:\\Program Files (x86)\\Notepad++\\Notepad++.exe"
fix(somefunction,editor=path)
or set notepad++ as default editor by changin R options for the current session i.e. :
path <- "C:\\Program Files (x86)\\Notepad++\\Notepad++.exe"
options(editor=path)
# from now on, all calls to fix and edit will open notepad++ as default editor...
fix(somefunction)
N.B.
If you want to set the new option as the default for all the next sessions, you should edit Rprofile.site script in RHome\etc path as explained here.
You could try something like this to create a temporary .csv and open in Excel.
Is there any way to use write() and write.table() so that the output file is in a different directory than the working directory? It tried setting the path to the output file before the filename, and just get an error message.
If you're using windows, R will know to go outside the current directory if it sees C:/ first (presumably other mounted drives too). With Macs it will go outside the current wd if it sees /. So:
Mac OS X:
write.table(foo, file="/users/name/folder/filename.ext")
Windows
write.table(foo, file="C:/users/name/folder/filename.ext")
Always test to make sure you have the path right first!
list.files("C:/...")
list.files("/....") #Give errors if path doesn't exist as typed
So if you're in /users/parent/subdir and you want to reference something in parent, you must type out the full path - write.table(foo, "parent/name.ext") will tell R to make a file: /users/parent/subdir/parent/name.ext.
Sure:
write.table(foo,file="../bar/baz.txt")
Or you can use absolute paths - nomenclature will depend on your operating system.