Uploading file when knitting has been complete - r

I would like to upload an html file automatically to a ftp server once it has been knitted.
I added this code to my rmd file:
RCurl::ftpUpload(here::here("rmd", "final.html"), Sys.getenv("MY_FTP_DETAILS"))
Obviously, there is an error since the HTML file hasnĀ“t been created yet.
Do you know of any work-around to get this file uploaded when the knitting process has been finalized?

Could you just use setwd(FTP server folder path) for the current project, and then have the knitted document saved to the current directory, which is the FTP server...
what do you get if you do
RCurl::ftpUpload("path/to/file/final.html"), Sys.getenv("MY_FTP_DETAILS"))

Related

Executable file not found

I have a python code that is taking some info from a website by selenium and then write them into a json file, after I created an '.exe' file by pyinstaller and run it I don't know where this json file is saved.
Where could be saved, should be in the initial directory?

Access R file functions from .Rmd file

I'm new in R and Rstudio and I'm making a little project. The fact is that i have on one hand an .R file with the code I want to execute. And on the other hand I've an .Rmd file that I should use to report my work, including the results of the execution of my code in the other file.
How can I access the results and/or functions from de .Rmd file to the .R file?
Thank you,
By default, your .Rmd file will have its working directory as wherever the .Rmd file is saved. You can use all of R's standard functions inside the .Rmd file, including source() to run a .R file. So if your files are in the same directory, you can include source("your_r_file.R") to run the .R file. If they are in different directories, you can use relative or absolute file paths (though you should try to avoid absolute file paths in case the .Rmd file is ever run on a different computer).
If you are using RStudio, I would strongly recommend using the "Projects" feature and the here package. The readme for the here package is quite good for explaining its benefits.
Source the R file in at the top of your .Rmd file like
```{r}
source("file-name.R")
```
and the functions/objects in that R file will be avilable

R issue when moving an rmd file from one project to another (working directory issue)

I have two projects in R. I've moved an .Rmd document from project 1 to project 2.
When I used the .Rmd file which I had moved to project 2 to try and read in some data I get the following error message:
cannot open file '"mydata.csv"': No such file or directoryError in file(file, "rt") : cannot open the connection.
This kind of error usually suggests to me it's a working directory issue, however when I run getwd() in the command line it's the correct working directory that is listed and points to where the csv is stored. I've also run getwd() within the rmd doc and again the wd is correct.
Does anyone else have this experience of moving one .Rmd file to another project and then it not working in the new project?
The code in the .Rmd file that I am trying to run is:
Data <- read.csv("mydata.csv", stringsAsFactors = T) and the data is definitely within the project and has the correct title, is a csv etc.
Has anyone else seen this issue when moving an RMarkdown document into another project before?
Thanks
This may not be the answer, but rmarkdown and knitr intentionally don't respect setwd(): the code in each block is run from the directory holding the .rmd file. So, if you've moved your .rmd file but are then using setwd() to change to the directory holding the data, that does not persist across code chunks.
If this is the issue, then one solution is to use the knitr options to set the root.dir to the data location:
opts_knit$set(root.dir = '/path/to/the/data')
See here.
Maybe not relevant but it seems to be the most likely explanation for what's happening here:
The project shouldn't really interfere with your code here. When opening the project it will set your working directory to the root location of the project. However, this shouldn't matter in this case since RMarkdown files automatically set the working directory to the location where the RMarkdown file is saved. You can see this when running getwd() once in the Console and once from the RMarkdown file via run current chunk.
The behavior is the same when the file is knitted. So except in the case when "mydata.csv" is in the same directory as the RMarkdown file, the code above won't work.
There are two workarounds: you can use relative or absolute paths to navigate to your data file. In a project I prefer relative paths. Let's say the rmd file is in a folder called "scripts" and your data file is in a folder called "data" and both are in the same project folder. Then this should work:
Data <- read.csv("../data/mydata.csv", stringsAsFactors = TRUE)
The other option, which I do not recommend, is to set the working diretory in the rmd file via:
opts_knit$set(root.dir = '../data/')
The reason why I wouldn't do that is because the working direcotry is only changed when knitting the document but using the rmd file interactivly, you have a different working directory (the location of the rmd file)
This is a great application of the here package for dealing with these types of issues.
here https://github.com/jennybc/here_here looks around the Rmd file (and all R files) for the .Rproj file and then uses that as an "anchor" to search for other files using relative references. If for instance you had the following project structure:
-data
|--mydata.csv
-src
|-00-import.R
|-01-make-graphs.R
|-02-do-analysis.R
-report
|--report.Rmd
-yourproject.Rproj
And you wanted to use mydata.csv in your report.Rmd you could do the following:
library(here)
dat <- read.csv(here("data", "mydata.csv"))
here will then convert this path to "~/Users/../data/mydata.csv" for you. Note that you have to be in the Rproject for this use case.
Here is such a great package and solves a lot of issues.

How to run an Rmd shiny markdown doc from online repo?

I have an interactive knitr/rmarkdown doc that uses shiny apps.
I want to place the .Rmd file on either my personal site or GitHub.
How would I be able to remotely run this file directly from online?
For example, when I run the Rmd file from my local machine, I use
rmarkdown::run("./IntroToShinyWorkshop.Rmd")
But when I try to run a file form online I get an error that the path doesn't exist (w/ one of the issues being that it merges the url with my wd).
run("IntroToShinyWorkshop.Rmd",dir = "https://raw.githubusercontent.com/theforestecologist/ShinyWorkshop/master/")
Error in run("https://raw.githubusercontent.com/theforestecologist/ShinyWorkshop/master/IntroToShinyWorkshop.Rmd") :
The directory 'C:/Users/theforestecologist/Documents/https:/raw.githubusercontent.com/theforestecologist/ShinyWorkshop/master' does not exist
In addition: Warning message:
In normalizePath(path.expand(path), winslash, mustWork) :
path[1]="https://raw.githubusercontent.com/theforestecologist/ShinyWorkshop/master": The filename, directory name, or volume label syntax is incorrect
Related: Once I do figure out how to run a file from an online repo, I'm also interested in how to change the working directory to that url so that I can utilize other files there.
For example, I have a wrapper function (.R file) I can source that will call my .Rmd file (instead of running the Rmd file directly) -- however, I get the error that I cannot change the wd in my wrapper function to access the .Rmd file online.

Read Doc file and file path from Browser

I have an issue that I want to read a DOC file in my system but the condition is
Suppose you are working on browser and download some PDF or DOC file ,then my program should run and get the path of that file and convert that doc file in binary format.
the file download path may be change because some time it download in default folder but some times you saves that file in other locations.
my concern is this that code should execute at download time and should get file path from download history and read that file.

Resources