假设我有一个如下所示的数据框,并且我使用插入UI元素创建了多个面板。在我的情况下,如何更新这些多个面板中的值。
我可以更新第一个面板的输入,但是当我尝试更新其他添加的面板时,事情变得更加混乱。假设在一个面板中我需要1985年地震的数据,在另一个面板中我需要1990年地震的数据。
我该怎么做?对于第一个面板,它可以正常工作,我可以在1985年至1990年的数据之间切换,但是当我添加新面板时,更新在任何包含第一个面板的面板中均无法正常工作。
使用CSS命令,删除按钮的位置也不会左右改变吗?
感谢任何人的帮助,因为R对我来说有点困难。
这是我尝试的代码,但是没有成功
# 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)
我希望所有插入的ui元素的更新都能正常工作。我将非常感谢为解决我的问题做出贡献的任何人。我正在这方面寻求任何帮助。
谢谢。
答案 0 :(得分:0)
这不是使用insertUI
来回答您的任务,但它更像是一种替代性的,更容易实现的建议。您可以使用软件包DT
,该软件包允许在闪亮的应用程序中就地编辑表格。在这里查看文档:{{3}},在这里查看演示闪亮的应用程序:https://rstudio.github.io/DT/。使用数据的简单示例:
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)