Programmatically load file into Rstudio - r

I'm developing R code whose output is an R script, is it possible to automatically load the generated file in a RStudio tab?
So far I've checked the rstudioapi, but I don't find any suitable function.
I've already tried:
file.show(stringr::str_c(name,".r"))
but file.show open the file using an external editor.

You can use source("path/to/script.R") to run an R script, and load all the data/variables into the global R environment
In order to just open the file in RStudio, without running it, you can use rstudioapi::navigateToFile("path/to/file.R")

Related

Not automatically downloading dropbox files when opening data in R

I recently bought a new Macbook pro (M1) and am trying to run R codes.
I open and run a R script that I have been working, but data such as csv, excel files, and RData in the dropbox are not loaded. It works only after I manually download the files in Finder, otherwise files are not automatically downloaded when trying to load in R.
Here is an example of the code and the error message
setwd('/Users/xxx/Dropbox/Data')
load('dta.RData')
Error in load("dta.RData") :
empty (zero-byte) input file
If I manually download the "dta.Rata" in finder (using Make Available Offline) and run the same code it works well.
Any solution?
I found that this
https://help.dropbox.com/installs-integrations/desktop/macos-12-monterey-support
I guess we should wait until Dropbox find the solution.

Sourcing external R scripts in Shiny App in R 4.0 vs 3.6.1

I have a shiny app that was working perfectly until the update to R 4.0. The app calls a couple of external helper R scripts, which are being sourced so the functions within will be available globally. The scripts are sourced outside of both the UI and Server components, so should be available in the global environment, I believe.
Warning in file(filename, "r", encoding = encoding) :
cannot open file 'RAW_DATA_ADAPT5_vAdDS.R': No such file or directory
Error in file(filename, "r", encoding = encoding) :
cannot open the connection
The error is unusual, because it's what I'd expect to get when the file isn't in the directory being specified. However, as I said, the script was working well as of R 3.6.1. Anyone have any insights as to what may have changed?
I did try to move the script sourcing into a globals.R file, but that didn't help.
edit:
The shiny version has also been updated since (1.5, now). The App is set up such that:
LaunchApp.R
MyBatchFileToRunLaunchApp.bat
App/app.R
App/R/<external R Scripts>.R
The source is being called as if(file.exists("R/<external R Script>.R)){source("R/<external R Script>.R")}
However, I also tested different versions where I moved the scripts into the same directory as app.R (with changing the source command) and then another test where I moved the block of code within app.R that does the sourcing into a global.R script in the same directory as app.R.
Solved through some combing of the Shiny documentation. Apparently, R will automatically call all *.R files in a 'R' directory that is with the app of interest. In my case, some of these files weren't supposed to be called by the app (they were there for validation purposes to be used outside the app, but called some of the same R scripts in that folder.
Solution was to remove all the excess .R files that weren't supposed be called by the app from the R directory.
The change was made in Shiny v1.4.0 (https://github.com/rstudio/shiny/blob/master/NEWS.md#shiny-140)
You can revert to the older behaviour by setting the following option options(shiny.autoload.r=FALSE), either in your script or in something like your .Rprofile file

RStudio woes: How do I set the base directory for "Create New Project"

I included the line
setwd("~/my project base dir")
within .Rprofile, but this has no effect on the file selector after I hit the top-right menu "Projects" to either open or create a new project.
I am running R 3.4.4, RStudio 1.1.442 on Ubuntu 16.04.4 LTS
Using setwd() is sending a command to R, not RStudio, so I wouldn't expect that to have any effect. R is run as a separate process by RStudio.
I haven't checked the source code, but it looks like RStudio always starts looking for an existing directory in the current project directory if you already have a project open, or your home directory if not. If you want to create a new directory, it seems to offer first to create it in the same place as it did last time.
I don't think there's any RStudio API command to change any of this. But how often to you need to create new projects, anyway?

R how to use files in the same packages

I am planing to do all my R scripts in the same package in order to move it easily between my friends.
What I have done is created an R packages using R studio and the following files have been automatically generated:
projectName.Rproj
DESCRIPTION
man
NAMESPACE
R
Read-and-delete-me
I created a new R script and I save it in R folder. Now I want to add a new R script that uses functions that have been defined in the first script.
I created this new script and I tried to use on of the functions that are located in the other script. I got error that the function is not defined.
What I tried to solve the problem
I used the source command in the beginning of the new script like this:
source('something.R')
I got error message that something.R doesn't exist.
What is the solution please to include functions that exist in a different scripts ** but in the same packages**?
NoteI don't want to use relative paths because I want the package to be as portable as possible
Thanks a lot
You appear to misunderstand how package works: by having a file with a function in the R/ directory, it is already visible to other code in the package.
If you want to make it available to other packages, you can control this via the NAMESPACE file. All this is well-documented in Writing R Extensions which comes with your copy of R, and a number of additional books and tutorials you could peruse.

Cannot load packages automatically in R -- what is the correct way to set up the .Rprofile script?

I am currently trying to change the settings of R running on my Mac so that it automatically loads packages I want. I created a .Rprofile file and dropped it into "~/", my home directory.
In the .Rprofile file, all I wrote was:
library("package1")
library("package2")
library("package3")
However, when I save and exit R, then go back, the packages have not installed by themselves. Is there something I am doing wrong? Thanks!

Resources