Start shiny app with input on shiny-server - r

My problem is as follows: Think of a shiny-app that handles a text input and presents results for this input. However, I want to limit the possibilities of my customers in a sense, that they can not give the text input manually (due to third-party-API-rate-limits) but only once externally. I retrieve the necessary input in a survey externally and I then want to redirect to my app with the input already given (so that they should not be able to play around with different other inputs).
So basically my question boils down to:
"How can I handle the input to a shiny app through a URL."
Let's say my app runs on
1.1.1.1:3838
which displays my app when I open it in a browser. Could I e.g. transform my app to handle URL-requests such as
1.1.1.1:3838/exampletext
(where 1.1.1.1 represents an IP Address) in a way that it then can process "exampletext" in its calculations and displays already prepared results (without the need of a textInput()-field)
I set up a shiny-server on an AWS EC2 instance but I am struggling to find any recommendations on how to build the infrastructure. I first intended to start a new app for each input in a bash script such as:
./app.R exampletext
and process it in the shiny app then like that:
args <- commandArgs(trailingOnly=TRUE)
textInput <- args[1]
However, I think there should be more clever ways to do this with a shiny app/on a shiny-server (apart from the fact that I don't have a real idea how this could really work out). Should I maybe consider a bash script to help me process the input and automatically start a script? In any case, I would be grateful if someone could at least provide me with some keywords to look for the matter appropriately. Thank you!
EDIT1: As proposed by #jyjek and also in this thread:
How do you pass parameters to a shiny app via URL. I could use an observer object that processes any changes in the URL to other objects. This solved the basic foundations of my problem, however, I could use a more static approach as my main goal is to create a non-changeable input. Therefore it is not necessary for me to adapt to changes in the URL, I rather prefer to give once a URL that should not be changeable.

Related

Interactive Document (takes input gives output) in Rpubs

Is it possible to publish an RMarkdown file to Rpubs that takes input and gives out put from the Rpubs site? I want to create a simple calculator related to my job that takes a few inputs and gives an output and, if possible, publish to Rpubs for people to view and use.
SHINY: I know this is what shiny is for, creating interactive apps, but I dont know it very well or how to implement an app I create for multiple people to use, or how to imbed the link to the app, and so on. Just trying to see if this is possible in things I already know how to use
RPubs is for HTML documents that don't require R calculations on the backend. In some instances, there can be some degree of interactivity e.g., brushing and linking plots, filtering data and having the filter propagate to a plot. These happen through the crosstalk package. However, it sounds like you need a shiny app that can take inputs and have R do some calculation of those on the back end. You could host your app on shinyapps.io
It's also possible, depending on how complicated the calculations are, that the entire app could be written in native javascript, which wouldn't require a server-side computation, so could be hosted on any website.

R processing on web site

I have a current project that consists of 3 parts:
An interface for clients to upload datasets from their equipment.
Process the uploaded datasets using R and some preset coefficients.
Display in a graph with a regression line, allowing the user to click points on the graph to remove them where needed and redraw the regression line automatically (after point removed).
Part 1: This is already done using PHP/Laravel. A simple upload and processing interface.
Part 3: I've been able to set this up in chart.js without any problems.
Part 2 is the sticking point at the moment. What I'd like is to be able to send the data to an rscript and just get the datapoints back so that I can display them. Can anyone give suggestions as to the best way to do this? Is there an api available? Or do I need to install software on the server (not an issue if I do, but I'm hoping to avoid the need to if possible)?
TIA
Carton
There is the package shiny to do everything in R (user side GUI and server side R processing). Since you did it in PHP already, you can either write an R script that is being executed with a shell call from PHP, or build an R REST API with plumber

R/Shiny: View full stack/execution log

I'm debugging a Shiny web app, and would like to see the entire control flow/execution path over the course of rendering and updating the generated website.
Is there a way to capture/print/dump-to-file every line of code that is executed in the process of rendering/updating a Shiny app? It would also be good (maybe better?) to see every line of R code parsed by the running R interpreter instance; I'm not concerned about length of this output, and would prefer to get things as verbosely as possible.
I have looked into the stack tracing Shiny functions but these seem to be intended for error catching/handling/reporting. The app is not generating errors/warnings, just setting some variables to NULL at some point when they shouldn't be, so I'm not sure if this is the right approach. These stack tracing functions also seem to be more localized, designed to operate within a given reactive variable/function/render rather than following the control/execution flow across differing reactives/rendering functions in an app.
This app is a large, company-internal app so I cannot give a MRE/MWE.
I finally found the profvis package that does more-or-less exactly what I want by taking a snapshot of the execution stack at a fixed time interval (default 10ms). I'm still working on using this tool to debug, but I believe this will get me there and would also be useful to others debugging Shiny apps that need more than browser() and/or reactlog.
Specifically, I have been doing:
#install.packages("profvis")
library(profvis)
exec_log <- profvis(runApp("myShinyApp"))
...interact with the myShinyApp web page enough to trigger the bug, then interrupt execution...
print(exec_log)

Use Julia to perform computations on a webpage

I was wondering if it is possible to use Julia to perform computations on a webpage in an automated way.
For example suppose we have a 3x3 html form in which we input some numbers. These form a square matrix A, and we can find its eigenvalues in Julia pretty straightforward. I would like to use Julia to make the computation and then return the results.
In my understanding (which is limited in this direction) I guess the process should be something like:
collect the data entered in the form
send the data to a machine which has Julia installed
run the Julia code with the given data and store the result
send the result back to the webpage and show it.
Do you think something like this is possible? (I've seen some stuff using HttpServer which allows computation with the browser, but I'm not sure this is the right thing to use) If yes, which are the things which I need to look into? Do you have any examples of such implementations of web calculations?
If you are using or can use Node.js, you can use node-julia. It has some limitations, but should work fine for this.
Coincidentally, I was already mostly done with putting together an example that does this. A rough mockup is available here, which uses express to serve the pages and plotly to display results (among other node modules).
Another option would be to write the server itself in Julia using Mux.jl and skip server-side javascript entirely.
Yes, it can be done with HttpServer.jl
It's pretty simple - you make a small script that starts your HttpServer, which now listens to the designated port. Part of configuring the web server is that you define some handlers (functions) that are invoked when certain events take place in your app's life cycle (new request, error, etc).
Here's a very simple official example:
https://github.com/JuliaWeb/HttpServer.jl/blob/master/examples/fibonacci.jl
However, things can get complex fast:
you already need to perform 2 actions:
a. render your HTML page where you take the user input (by default)
b. render the response page as a consequence of receiving a POST request
you'll need to extract the data payload coming through the form. Data sent via GET is easy to reach, data sent via POST not so much.
if you expose this to users you need to setup some failsafe measures to respawn your server script - otherwise it might just crash and exit.
if you open your script to the world you must make sure that it's not vulnerable to attacks - you don't want to empower a hacker to execute random Julia code on your server or access your DB.
So for basic usage on a small case, yes, HttpServer.jl should be enough.
If however you expect a bigger project, you can give Genie a try (https://github.com/essenciary/Genie.jl). It's still work in progress but it handles most of the low level work allowing developers to focus on the specific app logic, rather than on the transport layer (Genie's author here, btw).
If you get stuck there's GitHub issues and a Gitter channel.
Try Escher.jl.
This enables you to build up the web page in Julia.

R Shiny: how to share url with required filters for an analysis

I developed a Shiny app that I use to monitor some information
relevant to my company.
The app works fine but often if I notice something interesting in the data and I want to share it with my colleagues I have to send around an email
that says something on the lines of
Go to http://shiny.company.com:3838/
Select dates from "x" to "y"
In field "z" select this choices
and so on....
Clearly the best way would be to share a link that would make the Shiny app apply all the required settings.
I think I can partially solve the issue by using the function parseQueryString(session$clientData$url_search)
to set the relevant parameter from an url.
How I can update the url in the browser address bar when one selects values for the parameters? This way sharing a link becomes a matter of copying and pasting the address bar.
As suggested by #daattali, you are welcome to have a look at the shinyURL package which provides functionality to generate bookmarkable URLs which can be used to easily share a particular view state of your app.
Check out the live demo to see the package in action.

Resources