Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you pass parameters to a shiny app via URL

In web browsers you pass parameters to a website like

www.mysite.com/?parameter=1

I have a shiny app and I would like to use the parameter passed in to the site in calculations as an input. So is it possible to do something like www.mysite.com/?parameter=1 and then use input!parameter?

Can you provide any sample code or links?

Thank you

like image 205
user3022875 Avatar asked Sep 30 '15 17:09

user3022875


People also ask

Can I host Shiny app on my website?

If you host the app at its own URL, users can visit the app (and not need to worry about the code that generates it). If you are familiar with web hosting or have access to an IT department, you can host your Shiny apps yourself.

How do you read data to the Shiny app?

Your title asks about importing a data frame into shiny. That can be done by storing the data frame either as a binary file using the save() function or a csv file using write. csv() and having the shiny app read it in using load() for a binary file or read. csv() for a csv file.

Where do you put packages in the Shiny app?

Packages to load need to be declared at the start of the app. R file, at the server side of the app. R or alternatively at the modules file. At the modules, the libraries can be declared either outside of the fooUI and fooServer module functions or inside.

How do I run a Shiny app from the command line?

Open the app. R script in your RStudio editor. RStudio will recognize the Shiny script and provide a Run App button (at the top of the editor). Either click this button to launch your app or use the keyboard shortcut: Command+Shift+Enter (Control+Shift+Enter on Windows).


3 Answers

You'd have to update the input yourself when the app initializes based on the URL. You would use the session$clientData$url_search variable to get the query parameters. Here's an example, you can easily expand this into your needs

library(shiny)  shinyApp(   ui = fluidPage(     textInput("text", "Text", "")   ),   server = function(input, output, session) {     observe({       query <- parseQueryString(session$clientData$url_search)       if (!is.null(query[['text']])) {         updateTextInput(session, "text", value = query[['text']])       }     })   } ) 
like image 165
DeanAttali Avatar answered Oct 06 '22 07:10

DeanAttali


Building off of daattali, this takes any number of inputs and does the assigning of values for you for a few different types of inputs:

ui.R:

library(shiny)  shinyUI(fluidPage( textInput("symbol", "Symbol Entry", ""),  dateInput("date_start", h4("Start Date"), value = "2005-01-01" ,startview = "year"),  selectInput("period_select", label = h4("Frequency of Updates"),             c("Monthly" = 1,               "Quarterly" = 2,               "Weekly" = 3,               "Daily" = 4)),  sliderInput("smaLen", label = "SMA Len",min = 1, max = 200, value = 115),br(),  checkboxInput("usema", "Use MA", FALSE)  )) 

server.R:

shinyServer(function(input, output,session) { observe({  query <- parseQueryString(session$clientData$url_search)   for (i in 1:(length(reactiveValuesToList(input)))) {   nameval = names(reactiveValuesToList(input)[i])   valuetoupdate = query[[nameval]]    if (!is.null(query[[nameval]])) {     if (is.na(as.numeric(valuetoupdate))) {       updateTextInput(session, nameval, value = valuetoupdate)     }     else {       updateTextInput(session, nameval, value = as.numeric(valuetoupdate))     }   }   }   }) }) 

Example URL to test: 127.0.0.1:5767/?symbol=BBB,AAA,CCC,DDD&date_start=2005-01-02&period_select=2&smaLen=153&usema=1

like image 23
Jason S Avatar answered Oct 06 '22 05:10

Jason S


Shiny App: How to Pass Multiple Tokens/Parameters through URL

The standard delimeter for tokens passed through url to shiny app is the & symbol.

Example shiny app code:

server <- function(input, output, session) {
  observe({
    query <- parseQueryString(session$clientData$url_search)
    if (!is.null(query[['paramA']])) {
        updateTextInput(session, "InputLabel_A", value = query[['paramA']])
    }
    if (!is.null(query[['paramB']])) {
        updateTextInput(session, "InputLabel_A", value = query[['paramB']])
    }
  })
  # ... R code that makes your app produce output ..
}

Coresponding URL example: http://localhost.com/?paramA=hello&?paramB=world

Reference: parseQueryString Docs

like image 20
Jake Avatar answered Oct 06 '22 05:10

Jake