could not find bibliography file: citations.bib in r shiny - r

I'm having trouble with rmarkdown::render() recognizing a .bib file. I've tried putting it in the www file and also the directory from which the app is run with no success. I've had success locally by adding /users/blah/blah/citations.bib but when deploying this workaround it doesn't work (as it shouldn't). I've also looked into this Bibliography not working when rendering a R Markdown document within an R Shiny App, which works great for static htmls produced prior to deploying; however, my case involves the user creating a unique report while deployed and nullifies this option (I think...). Any suggestions would be much appreciated. Thanks.

Related

How to link a Shiny App uploaded in the browser and a Rmd?

I have developed a shiny app with some sections which produce some results. There is another section with the option of download, in HTML format, the plots done in the app. When I am in the R environment it works, but it works because i have the shiny app file .R and the R markdown file .Rmd in the same folder. But when I upload the app in the browser, this link between the shiny app and the r markdown is lost. Is there a way to link both so someone without the Rmd document and without even having R Studio downloaded can use the app and download the plots in an HTML?
I am trying to find some option in shinyapps.io (i guess there's the place where they can be linked) but with no success. Thank you.
Edition: I'm trying something like that https://shiny.rstudio.com/gallery/download-knitr-reports.html
But in this example, it doesn't matter where and what is the R Markdown file?
I have solved the problem. Being in the RStudio environment, when one want to publish the app, in the window "Publish to Server", in the box "Publish Files From", there must be the two files, the .R file where the shiny app code is and the .Rmd where the code for the HTML download is.

How to set RStudio R console working directory from r-markdown chunk

I am attempting to make a workflow as foolproof as possible using rmds and Git. Here, new rmd files and updates to old files are served via Git which is used by RStudio shell.
The issue is with different working directories between individual rmd files and the R console in RStudio. By default, the shell from which Git is used opens to the working directory of the R console. However, by default, rmd files work in the folder they are residing in, which is necessarily not the same as the R console working directory.
I am aware of the solution that sets the working directory of all cells within one rmd file (below), but this will not change the working directory of the R console, only chunks.
knitr::opts_knit$set(root.dir = "path/to/desired/wd")
Hence, I'd ask if anyone is aware of a way of forcing the R console working directory to match the rmd working directory. Ideally, this would be done within an rmd cell in a way that executing one cell would take care of matching the working directories automatically.

My shinydashboard app works on my machine but not on shinyapps.io

and thanks for any help!
I keep getting the same error message when trying to publish the app on shinyapps.io:
The application failed to start (exited with code 1).
I have already commented the setwd() and library(shiny) as I have learned from other posts, but so far no luck. This is the screenshot of the error.
I am new to this, so any support is greatly appreciated.
It looks to me like you are using an absolute file path in your script. shinyapps.io won't understand a file path specific to your machine.
Instead, try putting the files you need to read in a folder (e.g. 'InputFiles') and put that folder in the same place as your scripts. Change your scripts to refer to files using relative file paths like: 'InputFiles/file1.csv'.
When you run the code locally make sure to set the working directory to the same directory your scripts are in. When you publish to shinyapps.io make sure to include your scripts and the 'InputFiles' directory.
Here's a great explanation of how these work: https://docs.rstudio.com/shinyapps.io/Storage.html#Storage
The solution came to me after reading Thomas's post. I had an R script (which did all the statistics and plots for my dashboard) stored in the same folder where the shiny UI and server were stored. After moving this script file to a different folder, the problem was solved. I do not quite understand why this fixed the issue, but I hope this article helps people facing similar issues.

Generate R Markdown report using VBScript

Is there a way to generate an R Markdown report by calling that or other R scripts in a VBScript on windows 7?
My goal is for the user to be able to click the VBScript and have the R Markdown file generate so that the user does not have to interact with R.
I am able to run an R script from a VBScript (using Rscript.exe), but when it comes to an R Markdown file, there seems to be an issue with knitting/rendering.
I am able to source the R Markdown file from an R Script and I also used an R script to generate the HTML report using Markdown functions (see here), but if I call either of those from a VBScript, the report is not generated. I know the script is running because I also generate a text file (outside of the R Markdown file) as a test.
B/c I lack Admin rights, I don't believe I can use a batch file, but regardless, I think I would encounter the same issue. Thanks for any help/insight!

Set an .Rmd in a package to write files to the current project working directory

I have a .Rmd which I use to report on data quality in a number of different r projects. It would then split the data to remove subsets with missing data, and interpolate missing results where appropriate. It would do this via a write.csv command to a file path in the form of "./Cleansed_data/"
To make an example
open rstudio
go to the rhs 'project' menu , and select and make a new
project wherever you'd like
go to the lhs 'new script' drop down and
select 'new .Rmd'
change the output to .pdf and hit ok
in the last r
chunk include write.csv(mtcars, file = "mtcars.csv")
hit the 'knit
pdf' button, save the report as "writeFile.Rmd" to your project working directory, and
let it run.
Previously I moved this .Rmd from place to place, however now I would like to built it into an internal package. I have included it (as the documentation indicates to) into inst/rmd within the package directory.
In order to do this build or open any package you have access to
add the file to inst/rmd (create it if this doesn't exist)
rebuild the package
I then rebuild the package and open a new project. I load my new package and attempt to run the document via the render command using the system.file command to locate the .rmd like so
rmarkdown::render(input = system.file("rmd/writeFile.Rmd", package="MyPackage"),
output_file = "writeFile.pdf", output_dir = "./Cars/)
This will render the report from the package build into the folder from output_dir, however, there are a number of pitfalls here. First, if I omit the output_dir argument, the report will render into the package library, usually located in the libraries r installation in the c drive. This is however fixable.
What I can't get around is that when the .Rmd hits the write.csv() then (I believe) the .Rmd is being rendered in the package environment at the time, the working directory of which is the package library folder, not the current project directory.
The Questions
How can I inform the template in the package what the current working directory is for the rstudio project? I'm vaguely aware there might be a rstudio api package? I have nearly no understanding of what it is though, or if this would provide a solution.
If this is either outright impossible or just potentially a very bad idea how can I modify the workflow to successfully retrieve a number of r object outputs into the environment or the working directory, on the call to the report, without having to modify the report for each different project? Further, why specifically is this approach such a bad plan?
In order to close this off:
I have selected to keep the .Rmd included in the package. The .Rmd need to move and be versioned with the package as that holds the functions they use to run.
In order to meet my requirements I style the documents to grab the working directory via the rstudio api in the form.
write.csv(mtcars, file = paste0(rstudioapi::getActiveProject(), "mtcars.csv"))
Having tested #CL's answer, this also runs and is not dependant on Rstudio as an IDE, however I know that these documents will
Always be accessed via the rstudio IDE
Always be accessed from within a specific project
I fear (though have not tested) that there would be the potential for other impacts from setting the working directory for the file to be artificially booted into a different WD. Potentially this could be things like child documents I might want to include later, or other code that might need to be relevant to the file path of the package installation, not the project. In this way I think (If I interpreted Yuhui correctly) the r doc is still the centre of it's own universe. It just writes it's data into another one :)

Resources