RStudio show directory contents in file tab display window - r

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.

Related

If you run a file.to_csv code in Jupyter as opposed to Google Colab, where does the file go to be downloaded?

This is the input I'm running: file = pd.DataFrame({'column': x_test, 'target': y_test}) my_file.to_csv('file.csv', index=False).
In G Colab, the file appears in the files folder in the left column and you can download it there. I'm trying to figure how to download it in Jupyter?
Once you are done with your code as you mention in your question.
Click on File.
Select Open.
It will open a new tab where you find three menu File, Running, Clusters.
The csv file that you made by the name of "file.csv" you can find in the File section.

Making relative file paths for outputs

After looking at several similar questions on making relative file paths, I have some confusion on how to apply a relative file path for the outputs in my script. I have an application folder called "Master Map Change Report App" in which my script and outputs currently sit.
Right now this app sits on my local C: drive, but I need to move it to a shared drive so that other people can use it. I have all of the outputs going to 'reports' folder (see below). Currently the path to that folder looks like this:
report = "C:/Workspace/Sandbox/MapChangeProject/Master Map Change Report App/reports/map_change_report_{}.xls".format(today).
What I want is something like this:
report = ".../Master Map Change Report App/reports/map_change_report_{}.xls".format(today).
Of course, that doesn't quite work. What do I need to do here so that my outputs will always be within the 'Master Map Change ReportApp/reports' folder no matter where that folder is moved?
Your script is at "Master Map Change Report App". So your relative path for outputs should be "reports/map_change_report_{}.xls".format(today).
You may need:
import os
dirname = os.path.dirname(os.path.realpath('__file__'))
filename = os.path.join(dirname, 'reports','map_change_report_{}.xls'.format(today))

How to extract path from Windows shortcut in R?

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

How do I assign the working directory for rmarkdown/knitr interactive doc on shinyApps.io?

I have a fully functional interactive shiny doc using knitr/r markdwon.
However, when I try to publish (deploy) it to shinyApps.io, I get an error saying the .PNG file I try to incorporate into the doc (using readPNG from the png package) is unable to open.
I know the problem is related to working directories.
In my original code I assigned a working directory to my folder (i.e., "C:/Users/NAME/documents/...." that contains both my .rmd file and my .png file. However, obviously my folder doesn't exist on the shinyapps.io.
So how exactly do I set my working directory to open the .png file via my doc on shinyapps.io?
I can't find anywhere that explicitly walks through this process. Please help explain this simply/basically for me.
Example:
Assume I have a folder in "C:/Users/NAME/documents/Shiny" that contains 2 files: "shiny.rmd" and "pic.png". I want "pic.png" to be rendered in my shiny.rmd doc.
Currently I have:
---
title: "TroubleShoot"
output: html_document
runtime: shiny
---
```{r ,echo=FALSE,eval=TRUE}
library(png)
library(grid)
direct <- "C:/Users/NAME/documents/Shiny/"
img <- readPNG(paste0(direct,"pic.PNG"))
grid.raster(img)
```
How do I rewrite my code so it works on shinyApps.io?
Do I need to create and name folders in specific ways to place my 2 files into before deploying?
When you paste the c drive to direct, and read the direct into img, you are sending the app/markdown to your local drive. You need to use a relative path to the project. I would suggest something like:
direct <- "./"
Which uses the relative path, not the absolute path.
That said you might be able to skip that step entirely if the .png is in the same folder as the shiny object. just call the file name and format and leave it as that.
Also, check you use of ". Your syntax highlighting indicates your code won't run properly as it thinks some of your functions and variables are character strings because you've misplaced your quotes around read.png(). That's probably just a copy and paste typo though.
In summary, call the image via grid.raster("./pic.PNG"), and brush up on how working directories and relative paths work.
In the absence of knowing exactly how you're 'setting' your working directory:
In rStudio to correct and easiest approach is to make a 'New Project' for every New Project. You can do this as the first step in your project, or you can add a project file to an existing directory.
When you do this it automatically sets your working directory correctly. The way you seem to be doing it is not correct, you are merely supplying a 'path'. If you were to not want to use a project for some reason (I can't think of a case where that would be right), then you can always use setwd(), however, I believe this needs to be used in every session. A true project doesn't have that requirement.
This will then allow you to deploy your app properly as it will update the working directory to the one on the host machine at shinyapps.io when it is initialised there.
Finally, using the correct relative syntax "./path/to/my.file" to reference all your assets, images, data etc, should correct your issue as stated in the question.

Saving .R script File Using Script

I am using R Studio and I want to save my script (i.e., the upper left panel). However, the only ways that I can find to do it are by either clicking the blue floppy disk icon to save or using the drop down menu File > Save > name.R
Is there any way besides using these shortcuts to save the script to a .R file or is the shortcut the only way?
Thanks.
You can use rstudioapi::documentSave() to save the currently open script file to disk.
From the source documentation, one can see that it can be used in conjunction with the id returned with getActiveDocumentContext()$id to make sure the document saved is the one running the script.
For your intended use, try:
rstudioapi::documentSave(rstudioapi::getActiveDocumentContext()$id)
For future reference, here is the reference manual of rstudioapi:
https://cran.rstudio.com/web/packages/rstudioapi/rstudioapi.pdf
I'm not yet allowed to comment, but this refers to the comment above that this does not work with .rmd files:
rstudioapi::documentSave(rstudioapi::getActiveDocumentContext()$id)
I tried and in Rstudio Version 1.2.5042 it does seem to work.
Every new tab in R(created by ctrl+shift+n) can be independently saved by using ctrl+s in the respective tab. If you intend to rename the file though, you may do it as you would rename any file in windows(goto the file location and single click on the filename). Hope my answer was of some help!

Resources