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

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.

Related

invalid project layout error while deploying shiny app

I want to deploy a shiny app but i am getting this error.
Error in lint(appDir, appFiles, appPrimaryDoc) :
Cancelling deployment: invalid project layout.
The project should have one of the following layouts:
'server.R' and 'ui.R' in the application base directory,
'server.R' and 'www/index.html' in the application base directory,
'app.R' or a single-file Shiny .R file,
An R Markdown (.Rmd) or Quarto (.qmd) document,
A static HTML (.html) or PDF (.pdf) document.
'plumber.R' API description .R file
'entrypoint.R' plumber startup script
A tensorflow saved model
rsconnect::deployApp('Shared/Thomas/thomaps')
thomaps has an r script and a data folder with one excel sheet in it.

R studio will not remember my file path for a code chunk

When I run the following code I get an error.
library(tidyverse)
day <- read.csv("day.csv")[, -1]
glimpse(day)
Result:
Show in New Window
Error in file(file, "rt") : cannot open the connection
However when I specify the file path it works:
library(tidyverse)
setwd("~/UofUProjects/IS6489")
day <- read.csv("day.csv")[, -1]
glimpse(day)
I don't want to have to specify the file path for every single code chunk I run. Is this a setting I may have accidentally disabled? I tried deleting an re adding the file. I also tried clicking more and choosing set working directory and I still get the same error. I also tried updating Rstudio and R to the newest versions.
here is my working directory
The R markdown file I was using was saved in my download folder instead of my project folder. I was trying to specify the working directory folder with the R studio settings, and thought it would recognize however it did not work unless I actually saved the R markdown file in the project folder or used setwd(). I ran the code without the setwd() after I saved in the correct location and it worked just the way I wanted it to.

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.

R-Studio cannot read .txt file

I have a problem that I cannot solve, and it is the following: I am writing a document with R-Studio, but every time I try to run it, the program says to me that there is an error. The error apparently looks like to be linked to the fact that R studio is not able to load a table from a .txt file.
This is what I have done:
I opened R-Studio and I set the working directory (with the setwd() command) in the folder where the .txt file from which I want to source the table is located;
I opened the R-Markdown script file (with .Rmd extension) and I run it ('knit HTML' button);
R-Studio gave me the following error:
Errore in eval(expr, envir, enclos) : oggetto "mydata" non trovato; Calls: ... handle -> withCallingHandlers -> withVisible -> eval -> eval
How can I solve this? I cannot figure out why R-Studio is not able to read the table and load the data.frame, while the Console inside R-Studio is.
Thank you for any help you can provide.
When you knit a document in R-Studio, this happens in a separate environment. This means that variables that are available while you type in the console are not available when you knit a document. Similarly, when you set the working directory in the console, this has no effect on knitting a document.
So the solution to your problem could be to simply add the setwd() command to your .Rmd file.
In your R Markdown file, you have to specify the path to the txt file relative to the location of the Rmd file.

R Sweave output error

I'm using RStudio v0.96.331 with pdfTeX, Version 3.1415926-1.40.10 (TeX Live 2009/Debian).
I have a R project in the '/home/operacao/Myprojs/projName', which is my working directory.
Now, if i create a folder called 'reports' in '/home/operacao/Myprojs/projName/reports', and inside the sweave file (which is in the reports folder) use the code
setwd('/home/operacao/Myprojs/projName')
After loading some packages, i receive the error
Error in driver$finish(drobj) :
the output file 'my_report.tex' has disappeared
Calls: <Anonymous> -> <Anonymous>
Execution halted
But the file is in the folder, and the plots i made appear in the .pdf. The text output
does not appear.
Anyone knows why that happens? If i save the Sweave files in my directly in my working directory, everything works fine.
Thanks!
Probably RStudio requires you to set the working directory to the location which contains the Sweave file. Why do you need to set your working directory to another directory? You could use source to load any R code files which are in projName.

Resources