Background:
I like 'r'. I use Rstudio for it - it is a nice IDE. I use the revolution Analytics version of 'r', "revolution R Open".
I find that I type the same stuff in the annotation and structured programming regularly, and I want to save myself the re-typing.
Question:
How do I change the default file template so that the one I want, with some text already populated, comes up when I create a new blank R-script in Rstudio.
Clarifications:
I am not looking for this to be a manual process where I open one file, renaming it to a proper directory, and then work on it. I am looking to change the default so that this happens automatically.
Previous approach:
google
rstudio search (example)
search on stack-overflow
poking around rstudio menus/preferences
Thanks.
Quite late and not really a template but I think the solution is close: go to
Tools => Global Options => Code => Tab Editing => Snippets "Edit Snippets".
Example:
snippet header2
# Author:
# Date: `r paste(date())`
# --------------
# Author:
# Date:
# Modification:
# --------------
If you then type header {snippet} in a new script you get the text above with the date inserted automatically.
It was made possible to define user and system-wide templates for several file types with rstudio v1.3
A few gotchas:
it is necessary to create a 'templates' folder inside the .config directory
specific filetypes must be named according to this scheme
so, in this case, create a file called ~/.config/rstudio/templates/default.R or /etc/rstudio/templates/default.R for a user or system-wide .R file template, respectively.
I think this a 'less manual' solution than the accepted answer
As you are asking specifically for a Windows solution, you create a
templates folder (AppData/Roaming/RStudio/templates) and edit the default.R file.
# Create a templates folder
fs::dir_create(path = "~/AppData/Roaming/RStudio/templates")
# Create the file
fs::file_create("~/AppData/Roaming/RStudio/templates/default.R")
# Open the file in RStudio to edit it
usethis::edit_file("~/AppData/Roaming/RStudio/templates/default.R")
Now you can save the populated file and have created your new default R Script. Another possibility to not only keep this default template on your local machine would be to write a package for the sole purpose of containing a set of standardized default templates.
I've written a short post about it here.
Related
When using Rstudio, one can use File -> New File -> R markdown... to create a new R markdown file from a template, with YAML headers and some examples already buffered. Is there a function to replicate this without an IDE (in this case, saving the template Rstudio would buffer in a new file)?
I'd rather start my .Rmd file from template rather than from scratch, but not using an IDE.
rmarkdown::draft seems to do what you want. It creates a new file, based on a template. You can provide a link to a custom template. See the help page for details.
I am a new user of blogdown using Hugo. I would like to create a new post that includes R code to read a data file.
The data file is in my static folder, local path C:\mydir\myblogdown\static\data\savedrecs.txt. Since I was successful in referring to an image using a relative path like this, ![](/images\myimage.jpg), I tried reading the data using something similar for the data file, read.csv("/data\savedrecs.txt"), but that didn't work.
I started playing around with the list.files() function to see if I could find a relative path that worked in my local version of the post, list.files("../../static/data") worked, showing me ## [1] "savedrecs.txt".
I tried searching around other folks' blogdown repos on Github to see how they might have referred to a data file, but the only example I found referred to a data file using a URL.
I suspect that this may be due to the location of your data file. In my working example, the Rmd form of my blog post is in the directory, called p0bs/content/post. I also add my data file (a CSV in my case) to this directory.
I then treat the rest of the post as I would in a standard Rmarkdown website, with Rmd chunks (that are named without spaces). In your case, that code would include:
read.csv("savedrecs.txt")
I hope that helps you.
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.
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!
I've been using r-autoyas 'as-is' from what I got after installation. How do I customize it or install any other plugins that let me have templates for R documents (i.e. .R, .Rmd) whenever I create a new file?
For example, I want to put some boiler plate text:
Copyright info
Author
Date
File sections
The 'File sections' are different sections marker texts. For example:
## Library Import
## Functions
## Main Code
Maybe these can all be done with yas-snippets, but I don't know how to create custom snippet yet.