With shinyStorePlus v0.8, transfer browser link parameters to shiny inputs or outputs

Obinna N. Obianom

2024-08-17

Introduction

Imagine that your browser link looks something like this: 127.0.0.1:6316/?data=pressure&name=obinna&num=50&outt=wowowow

What if you’d like the transfer the value of data and name and num to a shiny input, and the value of outt to a shiny output. That can be accomplished using shinyStorePlus’ latest version.

Here is how you do it …

Step 1: Start with an already working application

Below is the code for an application to which we will integrate the shinyStorePlus package


# library
library(shiny)

if(interactive()) {
ui <- fluidPage(
  # Application title
  titlePanel("Transfer browser link parameters to shiny input"),

  # Sidebar with a slider input for number of bins
  selectInput(
    inputId = "datasetbin",
    label = "Choose an option",
    choices = c("rock", "pressure", "cars")
  ),
  textInput(
    inputId = "cd323",
    label = "Choose a name",
    value = "No name"
  ),

  numericInput(
    inputId = "number1",
    label = "Choose a number",
    value = 10
  ),
  
  htmlOutput("outputnum")
)

server <- function(input,output,session) {
}

shinyApp(ui = ui, server = server)
}

Step 2: Install and attach the shinyStorePlus 0.8 R package

The shinyStorePlus package is available on CRAN and can be installed as shown below

install.packages(shinyStorePlus)

Attach library

library(shinyStorePlus)

Now, the code you have should look like this …


# library
library(shiny)
library(shinyStorePlus)

if(interactive()) {
ui <- fluidPage(
  # Application title
  titlePanel("Transfer browser link parameters to shiny input"),

  # Sidebar with a slider input for number of bins
  selectInput(
    inputId = "datasetbin",
    label = "Choose an option",
    choices = c("rock", "pressure", "cars")
  ),
  textInput(
    inputId = "cd323",
    label = "Choose a name",
    value = "No name"
  ),

  numericInput(
    inputId = "number1",
    label = "Choose a number",
    value = 10
  ),
  
  htmlOutput("outputnum")
)

server <- function(input,output,session) {
}

shinyApp(ui = ui, server = server)
}

Step 3: Initialize by including the scripts required for processing the stores

You must now initialize for the package to work. Its as simple as inserting the function below within the fluidPage()

initStore()

Now, the code you have should look like this …


# library
library(shiny)
library(shinyStorePlus)

if(interactive()) {
ui <- fluidPage(
  
  #Initialize shinyStorePlus
  initStore(),
  
  # Application title
  titlePanel("Transfer browser link parameters to shiny input"),

  # Sidebar with a slider input for number of bins
  selectInput(
    inputId = "datasetbin",
    label = "Choose an option",
    choices = c("rock", "pressure", "cars")
  ),
  textInput(
    inputId = "cd323",
    label = "Choose a name",
    value = "No name"
  ),

  numericInput(
    inputId = "number1",
    label = "Choose a number",
    value = 10
  ),
  
  htmlOutput("outputnum")
)

server <- function(input,output,session) {
}

shinyApp(ui = ui, server = server)
}

Step 4: Setup the matching of parameters to input values

Match the Shiny input IDs to paramters in the link.

Now, the code you have should look like this …


# library
library(shiny)
library(shinyStorePlus)

if(interactive()) {
ui <- fluidPage(
  
  #Initialize shinyStorePlus
  initStore(),
  
  # Application title
  titlePanel("Transfer browser link parameters to shiny input"),

  # Sidebar with a slider input for number of bins
  selectInput(
    inputId = "datasetbin",
    label = "Choose an option",
    choices = c("rock", "pressure", "cars")
  ),
  textInput(
    inputId = "cd323",
    label = "Choose a name",
    value = "No name"
  ),

  numericInput(
    inputId = "number1",
    label = "Choose a number",
    value = 10
  ),
  
  htmlOutput("outputnum")
)

server <- function(input,output,session) {
  # for the inputs
  link2input(
    #example: shiny element ID = link parameter
    cd323 = "name",
    datasetbin = "data",
    number1 = "num"
    )
  #for the outputs
  link2input(
    outputnum = "outt",
    inputtype = "output"
  )
  
}

shinyApp(ui = ui, server = server)
}