RStudio / R: Where does sink() save my file - r

Im running an analysis in RStudio and use sink to store the output in a file because the line count is too large to manually copy it from the console. The code is:
sink("filename.txt")
...analysis
sink()
Now, where can I find the filename.txt file on my machine?
Thanks

If anyone else finds this post I recommend checking where your directory is before your sink statement.
getwd()
Just in case that is not where you want it to go.
I also keep a default output file destination and set that directory before my sink statement, and redirect after the statement.

Just found it myself, RStudio stores it under My Documents by default.

Related

How to specify where cronR saves output file

I have successfully run a simple cronR tutorial using the cronR add in within R Studio. Here is the following code that I have saved in a R script file:
library(glue)
current_time <- Sys.time()
print(current_time)
msg <- glue::glue("This is a test I am running at {current_time}.")
cat(msg, file = "test.txt")
The R script file is saved within a specific project directory. The log file when the job runs is also saved there. However, the output of the script file, test.txt, is saved in my home directory. I am on a Mac. In the tutorial, it was stated that this would happen, that cron would save any output in the home directory and that if I want to change the location that I have to "specify otherwise". However, the tutorial gives no instructions for how to do this and I am not sure if I am supposed to do this through the terminal in mac and if so how? Changing the file path in the script file (e.g. Documents/test.txt) changes nothing, as the test.txt file is still saved in the home drive. I suspect I have to make this change somewhere else but I am not sure where. Any help would be appreciated.
For anyone who runs into the same issue, I was able to solve my problem by using launchD. I followed this tutorial https://babichmorrowc.github.io/post/launchd-jobs/. It worked as anticipated and now my .txt file is saved to the correct place. There is also a tutorial there for cron jobs, though if you are on a mac, launchD is apparently the preferred method.

In Bluesky Statistics How do I write output to a csv file

I can't get write.csv or write.table to work in the r editor of BlueSky Statistics.
I usually just use this format in RStudio and it works perfectly.
write.csv(df, "zzz.csv")
Any hints?"
The default install location for BlueSky Statistics is 'C:\Program Files', where by default, there is no write permission (for creating or deleting files). Also, saving a file in the install location is not safe, as the file may get lost/deleted when the application is uninstalled. So it is always good to save your file(s) in your own folder(s) where you also have write permission.
In short, try to provide a writable location/path in write.csv() or other similar functions/commands.
See example below:
To save your file to the Desktop folder.
write.csv(df, "C:/Users/<YourUsername>/Desktop/zzz.csv")
Note: use forward slash(/) as a path separator.

Same R history from different workspace

I am new to R and I just figured out why my history did not contain all my previous commands. R create a .Rhistory file in each working directory.
I often change working directory and I would like to have the history of all my past sessions in the same file. Is there a simple way to do that ?
Thanks.
(I am on Mac OS 10.6 and I use Rstudio)
An easy way would be to manually save your history like this:
savehistory(file = "~/.Rhistory")
and then load it when you open an R command session:
loadhistory(file = "~/.Rhistory")
Otherwise you can edit your 'Rprofile.site' and add savehistory() and loadhistory() to the functions .Last and .First respectively.
More info about Rprofile.site: here
At startup, R will source the Rprofile.site file. It will then look for a .Rprofile file to source in the current working directory. If it doesn't find it, it will look for one in the user's home directory. There are two special functions you can place in these files. .First( ) will be run at the start of the R session and .Last( ) will be run at the end of the session.

Error in gzfile(file, "rb") - what should I do?

I'm going to write shortly, because my english is not perfect.
My code is:
explanatory=readRDS("explanatory_complete.Rds")
And I want knit pdf or word document, I receive this message:
Error in gzfile(file, "rb")- can't oppen connection
Where is problem? I set the right working directory.
Try with explanatory<-readRDS('filename.rds') this works for me.
Not sure if this is your case but I ran into
Error in gzfile(file, "rb")- can't open connection
when I tried to read a large file after it was downloaded using rsync. This was due to different file access permissions set by rsync than my Windows 10 system. The error was fixed in a local MobaXterm session using
chmod u+rwx filename
(change filename to your files)
The file likely doesn't exist in your working directory. This often happens to me when I have changed my working directory and forgotten to change it back.
Have you tried saving the new R/RMD file first?
I faced the same issue and problem was that, I was not saving the new file first therefore the R session was unable to understand the path for RDS file.
After I saved the file (in the same folder, but not a necessity) and ran the readRDS, it worked.
I had this issue. For me, it was being caused by an update.packages() call in a script my RMD document was sourcing. I'm sure there is a more elegant solution, but I just put a # in front of this line of code for knitting this RMD and then removed it again after
Check your exact file name and the working directly. it might be .rds missing in your saved file name.

Locate the ".Rprofile" file generating default options

In R and RStudio, I think I have messed around with the .Rprofile file a few times, and I currently am loading up an old version of it upon startup of R or RStudio, is there a way that I can quickly find the location of the file that is generating the default options?
Thanks
Like #Gsee suggested, ?Startup has all you need. Note that there isn't just the user profile file, but also a site profile file you could have messed with. And that both files can be found in multiple locations.
You could run the following to list existing files on your system among those listed on the page:
candidates <- c( Sys.getenv("R_PROFILE"),
file.path(Sys.getenv("R_HOME"), "etc", "Rprofile.site"),
Sys.getenv("R_PROFILE_USER"),
file.path(getwd(), ".Rprofile"),
file.path(Sys.getenv("HOME"), ".Rprofile"))
Filter(file.exists, candidates)
Note that it should be run on a fresh session, right after your started R, so that getwd() will return the current directory at startup. There is also the tricky possibility that your profile files do modify the current directory at startup, in which case you would have to start a "no-profile" session (run R --no-site-file --no-init-file) before running the code above.

Resources