Embedding multiple shiny apps in single Rmarkdown file using "shinyAppDir" function - r

I have created several shiny apps to embed in single Rmarkdown file.
I am using Amazon ec2 Ubuntu machine for hosting my shiny apps and rstudio. All the working apps are at /srv/shiny-server.
To do this, I create another folder for Rmarkdown single file in /srv/shiny-server. The individual chunks are running but Run Document commands is giving an error:
ERROR: cannot open the connection
I am using following R markdown code:
### App 1 goes here
```{r, echo=FALSE}
library(shiny)
shinyAppDir(
"/srv/shiny-server/App1",
options=list(
width="100%", height=550
)
)
```
### App 2 goes here
```{r, echo=FALSE}
library(shiny)
shinyAppDir(
"/srv/shiny-server/App2",
options=list(
width="100%", height=550
)
)
```
## Likewise ...

Not sure this will answer your question, but I encountered this problem and here's what I found. I'm running a shiny server on an Ubuntu EC2 instance. Also, I didn't create shiny apps the same way you did, instead I embedded interactive visualizations in R Markdown using ggvis and the shiny runtime like so:
---
date: "`r format(Sys.time(), '%d %B, %Y')`"
output:
html_document:
theme: readable
toc: yes
toc_depth: 3
runtime: shiny
---
Here are the two things that caused my problem:
Wrong paths to sourced code or data. You can provide absolute paths or paths relative to where your Rmd is located.
Using cache = TRUE in code chunks.
You can't cache things and when you do it causes the "cannot open the connection" error.

Related

Rmarkdown does not follow same paths as R script and console commands

In Rmarkdown, I cannot read files that I can read from the console and in an R script, because Rmarkdown is not following the same paths as my R scripts and console commands.
Here is a minimum reproducible example:
Create new project test.Rproj
Create a subdirectory called scripts
Run the following R Script scripts/test.R:
test <- as.data.frame(c(1, 2, 3))
dir.create("data")
write.csv(test, "data/test.csv")
rm(test)
test <- read.csv("data/test.csv")
Quit R, and reopen test.Rproj.
Knit the following Rmarkdown document (scripts/test.Rmd):
---
title: "test"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
_```
```{r read-data}
test <- read.csv("data/test.csv")
_```
Yields the following error: Quitting from lines 12-13 (test.Rmd)
Error in file(file, "rt") : cannot open the connection
Calls: ... withVisible -> eval -> eval -> read.csv -> read.table -> file
Execution halted
(Note, the backticks in the .Rmd file are properly specified -- I added underscores above so that the backticks appeared in the code block.)
Two seemingly related issues:
I can read the test.csv file via Rmarkdown if it is in the scripts subdirectory, rather than the data subdirectory.
When I run list.files() from the console or script, I receive of list of files in the top-level directory (i.e., where test.Rproj is located), including the data and scripts subdirectories. When I run list.files() from Rmarkdown, I get a list of files in the scripts subdirectory.
How can I fix this problem?
Session info:
R version 4.1.0 (2021-05-18)
RStudio version 1.4.1717
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Big Sur 11.5.1
Try something that looks like this as I am not sure of the nature of your `R Markdown.
test <- readRDS(here::here("data/test_data.rds"))
The bottom line is to use the here function from the here package.
When you knit a document in RStudio, by default the working directory is set to the current directory of the Rmd document (so that would be the "scripts" folder). Since the "scripts" folder does not contain the "data" directory, you get that error. You can change the default to use the project root directory if you prefer. That's an option in the RStudio Global Options menu.
See See https://bookdown.org/yihui/rmarkdown-cookbook/working-directory.html for more info

Rmarkdown Runs, but Produces Connection Error on Publishing

I have an Rmarkdown file which currently does not need to source() to other scripting files. There are currently two parameters in the YAML: a numeric input and a file upload for .CSVs
When I "knit with parameters" locally the file runs fine, I've even moved it out of my existing RProj to various other locations on my computer to make sure the working director doesn't matter and the place I'm grabbing .csv files from doesn't impact the knitting process.
When I attempt to publish to my RStudio Connect account I get the following two errors:
Error in file(file, "rt") : cannot open the connection
Calls: local ... withVisible -> eval -> eval -> read.csv -> read.table -> file
This is my current YAML for reference:
---
title: "CPR Report Card"
date: "`r format(Sys.time(), '%d %B, %Y')`"
output:
prettydoc::html_pretty:
theme: cayman
params:
data:
label: "Input Zoll Dataset:"
value: ".csv"
input: file
age:
label: "Age of Patient"
value: 0
input: numeric
min: 0
max: 17.75
step: .25
---
So, locally and across computers the file works fine. Seems like it should publish ok to RStudio Connect but currently isn't. This is my first foray into Connect, any help is greatly appreciated!
After communicating with the RStudio Connect support service, using read.csv() from an Rmarkdown file causes issues since the markdown does not have a reactive component to interact with the user's file system. If you wish to have this functionality it's best to look into developing a Shiny application.

Error in creating PDF file from R Markdown (Failed with error 43)

I am trying to create a PDF file using R Markdown. I received error 43 when I ran it. I tried create word file and HTML using R Markdown and it works fine. Only the PDF file does not work.
processing file: Test_New.Rmd
|...................... | 33%
ordinary text without R code
|........................................... | 67%
label: unnamed-chunk-1 (with options)
List of 1
$ tidy: logi TRUE
|.................................................................| 100%
ordinary text without R code
/Applications/RStudio.app/Contents/MacOS/pandoc/pandoc +RTS -K512m -RTS
Test_New.utf8.md --to latex --from
markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --
output Test_New.pdf --template/Library/Frameworks/R.framework/Versions/3.4/Resources/library/rmarkdown/rmd/latex/default-1.17.0.2.tex --highlight-style tango --latex-engine /usr/local/bin/pdflatex --variable graphics=yes --variable 'geometry:margin=1in'
output file: Test_New.knit.md
dyld: Library not loaded: /usr/local/lib/libjpeg.8.dylib
Referenced from: /usr/local/bin/pdflatex
Reason: image not found
pandoc: Error producing PDF
Error: pandoc document conversion failed with error 43
Execution halted
My code on R Markdown is the following.
---
title: "Test R Markdown"
author: "Sam Ng"
date: "September 1, 2017"
output:
pdf_document: default
html_document: default
word_document: default
---
# 1. R commands
Testing R commands
```{r,tidy=TRUE}
vector=c(1,2,3) # Create a new vector with three entries
max(vector) # Returns the maximum of a vector
min(vector) # Returns the minimum of a vector
```
I spent hours trying to resolve this. I installed Miktex, but was unable to get the packages directly from the package manager due to sitting behind a firewall at work. I finally was able to resolve this using the following steps:
Install Miktex
Create a folder to install Miktex packages, e.g. "C:\miktex_pkgs"
Go to the CTAN package archive and download the following .tar.lzma files:
-fancyvrb
-framed
-microtype
-miktex-zzdb1-2.9
-miktex-zzdb2-2.9
-mptopdf
-titling
-upquote
-url
Move the above .tar.lzma files to the "C:\miktex_pkgs" folder
Set your packages to download from the local repository: "C:\miktex_pkgs". To do this, go to:
"Miktex Settings" - this is an application
Click Packages
Change Package Repository to "C:\miktex_pkgs"
Go to "Miktex Package Manager" application and install the following packages by right-click, install:
List item
fancyvrb
framed
microtype
mptopdf
titling
upquote
url

Create documentation notebook for Shiny app files

I have a Shiny app with 3 R files (ui.r, server.r and global.r)
For normal .R documents I can just embed code like this:
#' ---
#' title: "User Interface for My Awesome App"
#' author: "Serban Tanasa"
#' date: "Current Version Updated - 2015.07.09"
#' output: html_document
#' ---
and so on and so forth, and assuming I've sprinkled comments around the whole script, I can press the handy "Compile Notebook" button in RStudio-Server:
...and compile it into a decent documentation. With the shiny app however, trying to compile the ui i get the following error.
output file: ui.knit.md
Error: path for html_dependency not provided
Execution halted
Is there some way to get around this?
This is for R 3.2.1, with RStudio Server Version 0.98.1103, and Shiny 0.12.0
Shiny is meant to be run as an application, not to be compiled into a document, I don't think that button is designed to work with Shiny files.

Deploy R markdown document on shinyapps.io

I'm trying to deploy an R markdown document on the shinyapp.io server.
I have followed the steps as described here.
However, step 8 indicates the presence of a "deploy" button. This button is missing.
As an alternative I have tried :
to open a new R script
Navigate to the Working Directory for my .Rmd file
Ran the following command: deployApp( appName = "Titanic")
Logs indicated everything was being uploaded correctly. However my destination page indicated 'not found'.
My questions:
Why did the 'deploy' button not show?
Why does my page indicate 'not found'?
Below you can find the header I used in the markdown document.
title: "KAGGLE - TITANIC SURVIVAL ANALYSIS"
output:
html_document:
toc: true
theme: spacelab
number_sections: true
runtime: shiny
Thank you in advance
Issue solved with R Studio version
Version 0.99.451 – © 2009-2015 RStudio, Inc.
!Note: In the newest version the deploy button has been replaced with Publish
This has not been adapted in the official shiny documentation yet. The screenshot below is the view you get once a first publish (formerly called deploy) action has been taken.

Resources