Updating the InsertUI elements

爷,独闯天下 提交于 2020-04-30 09:08:07

问题


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.

Thanks in advance.


回答1:


This is not answering your task using insertUI, but it is more of a proposal for an alternative, easier implementation. You can use the package DT which allows editing tables in place in a shiny app. Take look at the documentation here: https://rstudio.github.io/DT/ and an demo shiny app here: https://yihui.shinyapps.io/DT-edit/. A quick example using your data:

library(shiny)
library(DT)
# 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 <- basicPage(
  DTOutput("ee"),
  tags$hr(),
  h3("table after editing:"),
  tableOutput("edited")
)

server <- function(input, output) {
  output$ee <- renderDT({
    datatable(data = DT, editable = "row", filter = "top")
  })

  # edit a row
  observeEvent(input$ee_cell_edit, {
    DT <<- editData(data = DT, info = input$ee_cell_edit, 'ee')
    output$edited <- renderTable(DT)
  })
}

shinyApp(ui, server)


来源:https://stackoverflow.com/questions/57613327/updating-the-insertui-elements

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!