R Shiny app embedded in PowerPoint - r

Let's consider a simple r shiny application
# Global variables can go here
n <- 200
# Define the UI
ui <- bootstrapPage(
numericInput('n', 'Number of obs', n),
plotOutput('plot')
)
# Define the server code
server <- function(input, output) {
output$plot <- renderPlot({
hist(runif(input$n))
})
}
# Return a Shiny app object
shinyApp(ui = ui, server = server)
I would like to implement this application into my (Microsoft) PowerPoint presentation without losing its interactivity. I tried some things, but with little success:
ReporteRs package is promising, but I couldn’t find the way to directly include shiny application.
Another way is to install a gadget for live web pages into PowerPoint. In this case I can host my application on a web server and connect to that server with PowerPoint. But I don’t want that (there are sometimes problems with internet …).
Thirdly, I could create a presentation in R Markdown and later implement it into PowerPoint, but also didn’t find the way to do it.
So, are there any suggestions?

Related

Deploy shiny app that can call runApp() inside application itself (specifically for tabulizer package)

I'm trying to deploy a Shiny app that allows the user to upload a pdf document and extract a table from a selected page. For this I'm using the package tabulizer. A basic reproducible example:
library(shiny)
library(tabulizer)
ui <- fluidPage(
fileInput("report", NULL,buttonLabel = "Upload report"),
numericInput("page","Specify page number",value = 1),
actionButton("extract","Extract"),
verbatimTextOutput("data")
)
server <- function(input, output, session) {
generate_data <- reactive({
req(input$report)
# This locate_area function calls runApp() from the tabulizer package
area <- locate_areas(file = input$report$datapath,
pages = input$page,
widget = "reduced")
table <- extract_tables(file = input$report$datapath,
pages = input$page,
area = area)
return(table)
})%>% bindCache(input$page) %>% bindEvent(input$extract)
output$data <- renderPrint({
# Just for the sake of this example to show it works
generate_data()
})
}
shinyApp(ui = ui, server = server)
If I run this locally, the locate_area() will make the pdf page pop-up on my viewer in RStudio and all is well. However, if I publish the app it doesn't run after clicking the action button. I know the problem comes from the locate_area() as it essentially calls another runApp within the shiny app. I have tried using different widgets for locate_area() to no avail. Does anybody know a way to circumvent this issue?
Judging by the relevant issues - issue 15 and issue 53 - it appears that your best way to go is really to copy the functionality from the original tabulizer function into your own app, as currently the package does not provide an easy integration with other Shiny apps.

Run App gives no result R shiny app and crashes RStudio

I'm having an issue with the running of a R shiny app. Here's what I do:
open RStudio
load the shiny code (e.g. app.R)
set the wd
library(shiny)
press on "Run App"
Then nothing happens.
And if I try to terminate the execution, R does not answer anymore and says to force the closing of RStudio.
Here's one of my codes (I tried some, so I don't think this is the issue, but I report one anyway):
library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
titlePanel("Un'applicazione con uno slider"),
sidebarLayout(
h1("Sposta lo Slider!"),
sliderInput("slider1", "Spostami!", 0, 100, 0)
),
mainPanel(
h3("Valore dello slider:"),
textOutput("text")
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$text <- renderText(input$slider1)
}
# Run the application
shinyApp(ui = ui, server = server)
Do you have any advice to give me in order to make anything happen? I have no errors shown, so I don't know what to do...
I just see "runApp(...mypath.../app)" and blank space after it.
Thank you in advance.
Edit 1:
I also tried this, but nothing happened (as before):
library(shiny)
runExample("01_hello")
Edit 2: Copy-pasting the code directly in the console doen not work either
I was experiencing the same issue with R 4.1 on Windows 10. Updating all packages seems to have solved it:
update.packages(ask = FALSE, checkBuilt = TRUE)

Shiny app that fetches data via URL works locally but not on shinyapps.io

I wrote a simple shiny app to illustrate a problem I am having fetching data from a website. Here is the UI code:
library(shiny)
shinyUI(fluidPage(
# Application title
titlePanel("Test App"),
# Sidebar with slider inputs
sidebarLayout(
sidebarPanel(
actionButton("button", "Fetch Data")
),
mainPanel(
tabsetPanel(
tabPanel("Data", textOutput("crimelist"))
)
)
)
)
)
And here is the Server code:
library(shiny)
# Define server logic
shinyServer(function(input, output, session) {
# Get a list of the different types of crime
observeEvent(input$button, {
url <- "https://phl.carto.com/api/v2/sql?format=csv&q=SELECT
distinct rtrim(text_general_code) as text_general_code
FROM incidents_part1_part2
order by text_general_code"
crimelist <- read.csv(url(url), stringsAsFactors = FALSE)$text_general_code
output$crimelist <- renderText({crimelist})
})
})
The data being fetched is described at https://cityofphiladelphia.github.io/carto-api-explorer/#incidents_part1_part2.
When I run this shiny app in my local environment, it works perfectly. When publish it to shinyapps.io and run it, the application fails when I click the button to fetch the data. In the shiny log, it reports the following:
2017-10-11T14:46:31.242058+00:00 shinyapps[224106]: Warning in
open.connection(file, "rt") :
2017-10-11T14:46:31.242061+00:00 shinyapps[224106]: URL 'https://phl.carto.com/api/v2/sql?format=csv&q=SELECT distinct
rtrim(text_general_code) as text_general_code
2017-10-11T14:46:31.242062+00:00 shinyapps[224106]: FROM incidents_part1_part2
2017-10-11T14:46:31.242063+00:00 shinyapps[224106]: order by text_general_code': status was 'URL using bad/illegal format or missing URL'
2017-10-11T14:46:31.243062+00:00 shinyapps[224106]: Warning: Error in open.connection: cannot open connection
I am really stumped - is this a shiny server issue of some kind? If anyone out there has a clue, I am all ears!
I posted this question on multiple boards, and Joshua Spiewak on the Shinyapps.io Users Google group solved my problem:
You need to encode the URL in order for it to be valid, e.g. https://phl.carto.com/api/v2/sql?format=csv&q=SELECT%20distinct%20rtrim(text_general_code)%20as%20text_general_code%20FROM%20incidents_part1_part2%20order%20by%20text_general_code
shinyapps.io runs on Linux, so it is likely that curl is being used to fetch this URL. My guess is that you are using Windows locally, which uses a less strict mechanism that is tolerant of the spaces and newlines you have in the URL.
After making his suggested change, it works as expected now. Thanks, Joshua!

How to run user input as R code in a Shiny app?

I want to create a shiny application that has an input for writing some R function or Command, reads it through the ui.R then passes it to the server.R that executes that R command to display the results.
I spent hours searching about some example but couldn't find anything, I already know how to create Shiny apps using ui and server and pass the input values to server and work with them, but I have no idea if it's possible to create a shiny app like R where you can write the commands and return the results, any example or help would be appreciated.
Letting users run code in your app is bad practice, since it comes with great security risks. However, for development you might want to check this function from the shinyjs package by Dean Attali.
Example from the link:
library(shiny)
library(shinyjs)
shinyApp(
ui = fluidPage(
useShinyjs(), # Set up shinyjs
runcodeUI(code = "shinyjs::alert('Hello!')")
),
server = function(input, output) {
runcodeServer()
}
)
Some examples of why it is not such a good idea to include when deploying your app:
Try the input:
shinyjs::alert(ls(globalenv()))
or
shinyjs::alert(list.files())
I was able to find an alternative solution that doesn't require shinyjs -- wanted to restate Florian's concern that in general it is not a good thing (not secure) to let users run code in your Shiny app. Here is the alternative:
library(shiny)
library(dplyr)
ui <- fluidPage(
mainPanel(
h3("Data (mtcars): "), verbatimTextOutput("displayData"),
textInput("testcode", "Try filtering the dataset in different ways: ",
"mtcars %>% filter(cyl>6)", width="600px"),
h3("Results: "), verbatimTextOutput("codeResults"))
)
server <- function(input, output) {
shinyEnv <- environment()
output$displayData <- renderPrint({ head(mtcars) }) # prepare head(mtcars) for display on the UI
# create codeInput variable to capture what the user entered; store results to codeResults
codeInput <- reactive({ input$testcode })
output$codeResults <- renderPrint({
eval(parse(text=codeInput()), envir=shinyEnv)
})
}
shinyApp(ui, server)

How could I deploy a shiny app created using shinyApp() via shinyapps::deployApp( )?

I want to deploy (to the web) an application made in R/Shiny by a call to the shinyApp() function.
It is possible to make an app by a call to ShinyApp() as follows:
test_app = shinyApp(
ui = fluidPage(
numericInput("n", "n", 1),
plotOutput("plot")
),
server = function(input, output) {
output$plot <- renderPlot( plot(head(cars, input$n)) )
}
)
This retuns an object that represents the app and the app can run by printing that object. I wish to deploy the app that is made usingshinyapps::deployApp( test_app) however that gives me the following error:
Error in shinyapps::deployApp(test_app) :
appDir must be a single element character vector
This is because the deployApp function is expecting a directory not a shinyApp object. Presumably the information to build and therefore deploy the app is contained in the test_app object, but inspecting that object does not reveal much, and it seems to be the same for any app I create:
> str(test_app)
List of 4
$ httpHandler :function (req)
$ serverFuncSource:function ()
$ onStart : NULL
$ options : list()
- attr(*, "class")= chr "shiny.appobj"
>
The code to produce the app is not contained in any obvious way in that object. I suspect the answer may have something to do with R6 reference classes, which I do not understand.
Does anybody know how I might extract the information contained in the app from the test_app object in order to deploy it via the deployApp() function? (or an alternative approach)
I have posted this on the shinyApps users google group but got no response, so I'm trying again here.
The shinyApp command is not meant to be used for app construction, from its' help:
You generally shouldn't need to use these functions to create/run
applications; they are intended for interoperability purposes, such as
embedding Shiny apps inside a knitr document.
deployApp doesn't support shinyApp apps, as you probably found from ?deployApp. That said, it's an easy fix for your (and most) apps, by more or less pasting your commands into files called ui.R and server.R, wrapped in shinyUI() and shinyServer():
ui.R:
library(shiny)
shinyUI(fluidPage(
numericInput("n", "n", 1),
plotOutput("plot")
)
)
server.R:
library(shiny)
shinyServer(function(input, output) {
output$plot <- renderPlot( plot(head(cars, input$n)) )
}
)
Put the two files in a directory and then run deployApp("dir") after testing with runApp("dir")
If you have parts of your shiny app that are not part of server or UI (ie data preprocessing), you will need to paste them above the shiny call in the relevant file. If you are calling you shiny app with arguments, you can either hard code them above your shiny call, or integrate them as reactive values in the shiny itself.

Resources