...
Suppose I have a Data frame as given below and I created multiple panels using the insert UI elements. How do I, in my case update the values in these multiple panels.
I could update the inputs for the first panel but things get messier as I try to update the other added panels. Suppose in one panel I need data for Earthquake in 1985 and in the other panel I need data for Earthquake in 1990.
How do I do that? For the first panel it works fine and I could toggle between 1985 and 1990 data, but as I add new panel the updates doesn't work accordingly in any of the panels including the first one.
Also the the position of delete button not changing left or right with CSS commands, WHY?
Any help from anyone is appreciated as R is a bit getting difficult for me.
Here is the code I tried working on but without success
# Demo dataframe
DT <- data.frame(Year = c(1980,1985,1985,1990,1990,1995),
Events = c("Storm","Earthquake","Flood","Draught","Earthquake","Flood"),
Area_Loss = c(100, 200, 400, 500, 450,300),
Money = c(1000,2000,3000,4000,5000,6000))
#UI Logic
ui <- fluidPage( h4("Updating InserUIs",
inlineCSS("#delete_div{margin-top:1em;}"),
selectInput("events","Events",choices = as.character(DT$Events)),
tags$div(id = "Panels"),
actionButton("add","Add")
))
#Server Logic
server <- function(session, input, output){
vals <- reactiveValues(btn = 0)
#Adding and Removing buttons
observeEvent(input$add,ignoreNULL = FALSE,{
vals$btn <- vals$btn +1
insertUI(
selector = "#Panels",
ui = splitLayout(id = paste0("Selection",vals$btn), where = "afterEnd",
cellWidths = rep("33.33%",3),
selectInput(paste("year",vals$btn +1,sep = ""), "Year",
choices = as.numeric(DT$Year), selected = ""),
numericInput(paste("area",vals$btn +1,sep = ""), "Area", min = 0, max = 10000,
value ="", step = 1),
numericInput(paste("money",vals$btn +1,sep = ""), "Money", min = 0, max = 10000,
value = "", step =1),
div(id = "delete_div",actionButton(paste0("delete",vals$btn), "Delete"))))
observeEvent(input[[paste0("delete",vals$btn)]],{
shiny::removeUI(selector = paste0("#Selection",vals$btn))
vals$btn <- vals$btn - 1
})
})
#For Updating the inserted UIs
Year_Value <- reactive({
Year <- c(input[["year"]])
if(vals$btn>0){
for(i in 1:vals$btn){
Year <- c(Year,input[[paste0("year", i+1)]])
}
Year <- paste(Year,collapse = "\n")
}
})
#Updates based on Year and Events
observeEvent(input$events,
updateSelectInput(session,paste("year",vals$btn +1,sep = ""), "Year",
choices = as.numeric(DT$Year)[DT$Events == input$events], selected = ""))
observeEvent(Year_Value(),
updateNumericInput(session,paste("area",vals$btn +1,sep = ""),
"Area",min= 0, max= 50000,value = DT$Area_Loss[DT$Year == Year_Value() &
DT$Events== input$events] ,step = 0.1))
observeEvent(Year_Value(),
updateNumericInput(session,paste("money",vals$btn +1,sep = ""),
"Money",min= 0, max= 50000,value = DT$Money[DT$Year == Year_Value() &
DT$Events == input$events],step = 0.1))
}
shinyApp(ui,server)
I want my updates to work properly for all the inserted ui elements. I will be extremely grateful to anyone contributing towards solving my problem. I am looking for any kind of help in this regard.
I searched for this particular topic for quite a few days but didn't get anything on it. The task should have been a trivial one but it is not straightforward as I thought it would be. I hope that R studio community can help me this regard which is quite an urgent for me and I have already spent quite a lot of time on it
Thanks in advance.