Create a shiny app such that when a user refreshed the app, they can still see the input they entered.
Integrating the shinyStorePlus package into your already developed application is very simple and can be accomplished in just few lines of code. Neat, right?
Well, get started …
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("shinyStorePlus by Obi Obianom"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30),
selectizeInput(
'foo', label = NULL, choices = state.name,
options = list(create = TRUE),
multiple = TRUE
),
textInput("redd","What if I?",value = "Wayword...")
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
)
server <- function(input,output,session) {
# make sure the input, output, session arguments are present !IMPORTANT
output$distPlot <- renderPlot({
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white',
xlab = 'Waiting time to next eruption (in mins)',
main = 'Histogram of waiting times')
})
}
shinyApp(ui = ui, server = server)
}
shinyStorePlus
R
packageThe 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("shinyStorePlus by Obi Obianom"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30),
selectizeInput(
'foo', label = NULL, choices = state.name,
options = list(create = TRUE),
multiple = TRUE
),
textInput("redd","What if I?",value = "Wayword...")
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
)
server <- function(input,output,session) {
# make sure the input, output, session arguments are present !IMPORTANT
output$distPlot <- renderPlot({
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white',
xlab = 'Waiting time to next eruption (in mins)',
main = 'Histogram of waiting times')
})
}
shinyApp(ui = ui, server = server)
}
You must 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("shinyStorePlus by Obi Obianom"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30
),
selectizeInput(
"foo",
label = NULL, choices = state.name,
options = list(create = TRUE),
multiple = TRUE
),
textInput("redd", "What if I?", value = "Wayword...")
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
)
server <- function(input, output, session) {
# make sure the input, output, session arguments are present !IMPORTANT
output$distPlot <- renderPlot({
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
# draw the histogram with the specified number of bins
hist(x,
breaks = bins, col = "darkgray", border = "white",
xlab = "Waiting time to next eruption (in mins)",
main = "Histogram of waiting times"
)
})
}
shinyApp(ui = ui, server = server)
}
Setup the stores by declaring a unique app id and including this in the setup function as shown below
appid = "applicatio32n501"
setupStorage(appId = appid,inputs = TRUE)
Now, the code you have should look like this …
# library
library(shiny)
library(shinyStorePlus)
if (interactive()) {
ui <- fluidPage(
# Initialize shinyStorePlus
initStore(),
# Application title
titlePanel("shinyStorePlus by Obi Obianom"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30
),
selectizeInput(
"foo",
label = NULL, choices = state.name,
options = list(create = TRUE),
multiple = TRUE
),
textInput("redd", "What if I?", value = "Wayword...")
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
)
server <- function(input, output, session) {
# make sure the input, output, session arguments are present !IMPORTANT
output$distPlot <- renderPlot({
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
# draw the histogram with the specified number of bins
hist(x,
breaks = bins, col = "darkgray", border = "white",
xlab = "Waiting time to next eruption (in mins)",
main = "Histogram of waiting times"
)
})
# insert at the bottom !!!IMPORTANT
appid <- "applicatio32n501"
setupStorage(appId = appid, inputs = TRUE)
}
shinyApp(ui = ui, server = server)
}
Paste the final R code into your console and run. Change several inputs when the application loads, see the effect. Now, refresh the page. You should see that your previous inputs are preserved.
Documentation: https://shinystoreplus.obi.obianom.com Code and report issues: https://github.com/oobianom/shinyStorePlus