问题描述
我有10个编辑" actionButtons.在每个编辑"操作按钮上,出现三个selectInputs.单击selectInputs下方的"Save"按钮后,我想记住选择的值.如果我转到第2行->单击编辑"->选择值->单击保存",然后返回第1行,它将不记得第1行中先前选择的值.
I have 10 'Edit' actionButtons. On each 'Edit' action buttons appears three selectInputs. After I click on 'Save' button below selectInputs, I want to remember selected values. If i go to row 2 -> click 'Edit' -> select values -> click 'Save' and go back to row 1, it does not remember previously selected values in row 1.
所以我想对保存"按钮执行两个操作:
So I want to have two actions on 'Save' button namely:
- 存储给定行"的选定值(因此,当我再次单击相同的编辑"按钮时,我将看到先前选定的值)
-
在我点击保存按钮后,所有的selectInputs都会消失
- store selected values for given 'Row' (so when i click again on the same Edit button I will see previously selected values)
make all selectInputs disapear after i click on save button
图书馆(闪亮)
ui <- shinyUI(fluidPage(
titlePanel("Update Select Inputs"),
mainPanel(
tags$head(tags$script(HTML("$(document).on('click', '.needed', function () {
Shiny.onInputChange('last_btn',this.id);
});"))),
column(width = 6,
uiOutput('ValuesAndButtons')),
column(width = 6,uiOutput('Details'))
)
)
)
server <- shinyServer(function(input, output) {
output$ValuesAndButtons <- renderUI({
lapply(
c(1:10),
function(i){
fluidRow(
column(
width = 4,
tags$span(i)
),
column(
width = 3,
tags$button(
type = "button",
id = paste0('ActionButton', i),
class="btn action-button needed",
"Edit"
)
)
)
}
)
})
output$Details <- renderUI ({
req(input$last_btn)
fluidRow(
lapply(
c(1:3),
function(i){
fluidRow(
fluidRow(
column(
width = 5,
tags$span(i)
),
column(
width = 5,
selectInput(
paste0("Details", i),
label = NULL,
choices = c("","a","b","c"),
selected = ""
)
)
)
)
}),
fluidRow(
tags$button(
type = "button",
id = "Save",
class = "btn action-button",
href = "#",
"Save"
)
)
)
})
})
# Run the application
shinyApp(ui = ui, server = server)
推荐答案
您可以通过将值存储到仅存储一个session
you can do it by storing values into reactivevValues
values stored onlyfor one session
library(shinyjs) # needed for hide and show
ui <- shinyUI(fluidPage(
titlePanel("Update Select Inputs"),
mainPanel(
useShinyjs(),
tags$head(tags$script(HTML("$(document).on('click', '.needed', function () {
Shiny.onInputChange('last_btn',this.id);
});"))),
column(width = 6,
uiOutput('ValuesAndButtons')),
column(width = 6,hidden(wellPanel(id="det",
lapply(1:3,function(i){
fluidRow(
selectInput(
paste0("Details", i),
label = i,
choices = c("","a","b","c"),
selected = ""
)
)
}),
fluidRow(
actionButton("Save","Save")
)
))
))
)
)
server <- shinyServer(function(input, output,session) {
saved_values=reactiveValues(data=NULL)
output$ValuesAndButtons <- renderUI({
lapply(1:10,function(i){
fluidRow(
column(
width = 4,
tags$span(i)
),
column(
width = 3,
actionButton(paste0('ActionButton', i),"Edit",class="needed" )
)
)
}
)
})
observe({
if(!is.null(input$last_btn)){
observeEvent(input[[input$last_btn]],{
shinyjs::show("det")
}
)
}
})
observeEvent(input$last_btn,{
ll<-saved_values[[input$last_btn]]
shinyjs::show("det")
if(!is.null(ll) ){
for(i in 1:length(ll)){
updateSelectInput(session = session,inputId = names(ll)[[i]],selected = ll[[i]])
}
}else{
shinyjs::reset("det")
}
})
observeEvent(input$Save,{
saved_values[[input$last_btn]]=lapply(names(input)[which(substring(names(input),1,7)=="Details")],function(i){
input[[ i]]
})
names(saved_values[[input$last_btn]])=names(input)[which(substring(names(input),1,7)=="Details")]
shinyjs::hide("det")
})
})
# Run the application
shinyApp(ui = ui, server = server)
这篇关于R Shiny-将选择的值保存在selectInput中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!