r-如何使滑块输出独立于内部的反应变量



在这个闪亮的应用程序中,3个selectInputs是链接的,如果我们在第一个selectInput中选择一个国家,则所选国家不会出现在第二个中,第三个也是如此。未选择的2个国家出现在幻灯片的标签中(反应变量(当您凝视滑块输入,并在上面的selectInputs中修改任何国家/地区时,滑块输入不会保留以前选择的值。

我的问题是,如何保持滑动输入独立于修改selectInput?感谢您的帮助

ui <- fluidPage(
sidebarLayout(

sidebarPanel(
uiOutput("BoxLPbilan"),
uiOutput("BoxLPX"),
uiOutput("BoxLPY"),
uiOutput("Balance_country1"),
uiOutput("Balance_country2")
),
mainPanel (

)
)
)
server <- function(input, output) {
countries<-reactive({
c('FR','DE','BE','NL','AT')
})
output$BoxLPbilan <- renderUI({
# as.character: to get names of levels and not a numbers as choices in case of factors
selectInput("countrybilan",label = tags$span(style="color: blue;","Select the balance country") ,countries())
})
liste_paysX <- reactive (
{
w=countries()
sort(w[!w %in% input$countrybilan])
})

output$BoxLPX <- renderUI({
# as.character: to get names of levels and not a numbers as choices in case of factors
selectInput("countryX", label = tags$span(style="color: red;","Select the country of the X axis"),liste_paysX())
})

liste_paysY <- reactive (
{
w=countries()
w=(w[!w %in% input$countrybilan])
sort(w[!w %in% input$countryX])
})

output$BoxLPY <- renderUI({
# as.character: to get names of levels and not a numbers as choices in case of factors
selectInput("countryY", label = tags$span(style="color: red;","Select the country of the Y axis"),liste_paysY())
})

remaining_countries <- reactive (
{
w=countries()
w=(w[!w %in% input$countrybilan])
w=(w[!w %in% input$countryX])
sort(w[!w %in% input$countryY])

})
output$Balance_country1 <- renderUI({
sliderInput(inputId = "remaining_country1",
paste(remaining_countries()[1]," balance"),
min = -10000,
max = 10000,
step = 100,
value = 0)

})

output$Balance_country2 <- renderUI({

sliderInput(inputId = "remaining_country2",
paste(remaining_countries()[2]," balance"),
min = -10000,
max = 10000,
step = 100,
value = 0)

})


}
shinyApp(ui = ui, server = server)

您可以使用以下技巧-

创建一个reactiveValues来存储sliderInput的当前值,并传递默认值sliderInput作为存储值。

library(shiny)
ui <- fluidPage(
sidebarLayout(

sidebarPanel(
uiOutput("BoxLPbilan"),
uiOutput("BoxLPX"),
uiOutput("BoxLPY"),
uiOutput("Balance_country1"),
uiOutput("Balance_country2")
),
mainPanel (

)
)
)
server <- function(input, output) {

rv <- reactiveValues(val1 = 0, val2 = 0)

countries<-reactive({
c('FR','DE','BE','NL','AT')
})
output$BoxLPbilan <- renderUI({
# as.character: to get names of levels and not a numbers as choices in case of factors
selectInput("countrybilan",label = tags$span(style="color: blue;","Select the balance country") ,countries())
})
liste_paysX <- reactive (
{
w=countries()
sort(w[!w %in% input$countrybilan])
})

output$BoxLPX <- renderUI({
# as.character: to get names of levels and not a numbers as choices in case of factors
selectInput("countryX", label = tags$span(style="color: red;","Select the country of the X axis"),liste_paysX())
})

liste_paysY <- reactive (
{
w=countries()
w=(w[!w %in% input$countrybilan])
sort(w[!w %in% input$countryX])
})

output$BoxLPY <- renderUI({
# as.character: to get names of levels and not a numbers as choices in case of factors
selectInput("countryY", label = tags$span(style="color: red;","Select the country of the Y axis"),liste_paysY())
})

remaining_countries <- reactive (
{
w=countries()
w=(w[!w %in% input$countrybilan])
w=(w[!w %in% input$countryX])
sort(w[!w %in% input$countryY])

})


output$Balance_country1 <- renderUI({
sliderInput(inputId = "remaining_country1",
paste(remaining_countries()[1]," balance"),
min = -10000,
max = 10000,
step = 100,
value = rv$val1)

})

output$Balance_country2 <- renderUI({

sliderInput(inputId = "remaining_country2",
paste(remaining_countries()[2]," balance"),
min = -10000,
max = 10000,
step = 100,
value = rv$val2)

})


observe({
req(input$remaining_country1, input$remaining_country2)
rv$val1 <- input$remaining_country1
rv$val2 <- input$remaining_country2
})

}
shinyApp(ui = ui, server = server)

最新更新