I am using flexdashboard
in shiny to create my application. I have tried my best to add the input fields in shiny by allowing the user to click on a button. However, the problem I am having right now is everytime I add input fields in my shiny app the values in those input fields get reset. I haven't been able to implement a button and remove the input fields that I added in the app. So any input to efficiently add / remove input fields in shiny is highly appreciated. I want to add /remove three fields at once as shown in my application. May be there is a elegant may to perform what I am doing in my app.
The reproducible example is as follows:
---
title: "Shiny Flexboard Testing"
output:
flexdashboard::flex_dashboard:
orientation: rows
social: menu
source_code: embed
runtime: shiny
---
```{r global, include=FALSE}
# load data in 'global' chunk so it can be shared by all users of the dashboard
library(ggplot2)
library(mgcv)
library(magrittr)
library(plotly)
library(directlabels)
library(shinydashboard)
Column {.sidebar data-width=350}
-----------------------------------------------------------------------
### Plan View Controls
# Inline pricelow and pricehigh
div(style="display: inline-block;vertical-align:top; width: 70px;height:10px",
shiny::textInput('pricelow', 'Min price', value = 0))
div(style="display: inline-block;vertical-align:top; width: 70px;",
shiny::textInput('pricehigh', 'Max price', value = 30))
### Time Series Controls
fluidRow(column(4, textInput('num_ts','No of TS', value = 2)),
column(4, shiny::textInput('ts_pricelow', 'Min price', value = 0)),
column(4, shiny::textInput('ts_pricehigh', 'Max price', value = 30)))
fluidRow(column(3, actionButton("addInput","Add Input", style ='font-size:80%; padding:4px')),
#column(2, actionButton("resetInput","Reset Input", style ='font-size:80%; padding:4px')),
column(3, checkboxInput(inputId = 'plotpoints', "MapView", value = FALSE)),
column(3, checkboxInput(inputId = 'ts_facet', "Facet", value = FALSE)),
column(3, checkboxInput(inputId = 'facet_free', "Y Free", value = FALSE)))
uiOutput("inputs")
Column {.tabset}
------------------------------------------------------------------------
### Process addInput
```{r}
ids <- NULL
observeEvent(input$addInput,{
print(ids)
if (is.null(ids)){
ids <<- 1
}else{
ids <<- c(ids, max(ids)+1)
}
output$inputs <- renderUI({
tagList(
lapply(1:length(ids),function(i){
# Create a div that contains 3 sub divs
div(
# Display option to provide I of Cell
div(style="display: inline-block;vertical-align:top; width: 80px",
shiny::textInput(paste0('cell_i_',ids[i]), 'I index', value = 10)),
# Option to provide J of cell
div(style="display: inline-block;vertical-align:top; width: 80px",
shiny::textInput(paste0("cell_j_",ids[i]), 'J index', value = 34)),
# Option to provide K of cell
div(style="display: inline-block;vertical-align:top; width: 80px",
shiny::selectInput(paste0("cell_k_",ids[i]), 'Layer', c(1:10)))
)
})
)
})
})