首页 > 解决方案 > 在 Shiny 中选择选项时删除滑块

问题描述

我想帮助解决以下问题:

下面的可执行代码显示了插入 excel 文件时的一组条件(可以是任何条件)。您有“是”或“否”选项。如果请求“否”选项,则会出现以下选项:更改过滤器或更改集群编号,如附件中所示。如果我选择更改集群编号的选项,则会显示一个滑块。但是,如果我按 Yes 选项,则应该删除 Slider,但是它没有完成(附加)。因此,我想对此进行调整。

library(shiny)
library(shinythemes)
library(readxl)

df<-structure(list(Properties = c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35), 
                   Latitude = c(-23.8, -23.8, -23.9, -23.9, -23.9,  -23.9, -23.9, -23.9, -23.9, -23.9, -23.9, -23.9, -23.9, -23.9, -23.9, -23.9, -23.9, -23.9, -23.9,-23.9,-23.9,-23.9,-23.9,-23.9,-23.9,-23.9,-23.9,-23.9,-23.9,-23.9,-23.9,-23.9,-23.9,-23.9,-23.9), 
                   Longitude = c(-49.6, -49.6, -49.6, -49.6, -49.6, -49.6, -49.6, -49.6, -49.6, -49.6, -49.7, -49.7, -49.7, -49.7, -49.7, -49.6, -49.6, -49.6, -49.6,-49.6,-49.6,-49.6,-49.6,-49.6,-49.6,-49.6,-49.6,-49.6,-49.6,-49.6,-49.6,-49.6,-49.6,-49.6,-49.6), 
                   Waste = c(526, 350, 526, 469, 285, 175, 175, 350, 350, 175, 350, 175, 175, 364, 175, 175, 350, 45.5, 54.6,350,350,350,350,350,350,350,350,350,350,350,350,350,350,350,350)), 
              class = "data.frame", row.names = c(NA, -35L))

ui <- bootstrapPage(
  navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
             "Cl", 
             tabPanel("Solution",
                      fileInput("data", h3("Import excel")), 
                      sidebarLayout(
                        sidebarPanel(
                        
                          conditionalPanel(
                            "output.fileUploaded == true",
                            tags$hr(style="border-color: black;"),
                            tags$p(h3("Are you satisfied?")),
                            radioButtons( "satisfaction","", choices = list("Yes" = 1,"No " = 2),selected = 1)),
                          
                          conditionalPanel(
                            "input.satisfaction == '2'",
                            selectInput("naosatisf", h4("Choose a option below:"), 
                                        choices = c("no option selected" = "","Change the filters" = 1, "Change the cluster number" = 2), selected = "")),
                          
                          conditionalPanel(
                            "input.naosatisf == '2'",  
                            sliderInput("Slider", h5(""),
                                        min = 2, max = 31, value = "")),
                        
                        ),
                        mainPanel(
                          tabsetPanel(      
                            tabPanel("Solution", plotOutput("ScatterPlot"))))
                      ))))

server <- function(input, output, session) {
  
  v <- reactiveValues(df = NULL, clear = FALSE)
  
  observeEvent(input$data, {
    v$df <- read_excel(input$data$datapath)
    v$clear <- TRUE
  })
  
  output$fileUploaded <- reactive({
    v$clear
  })
  
  outputOptions(output, "fileUploaded", suspendWhenHidden = FALSE)
  
}

shinyApp(ui = ui, server = server)

在此处输入图像描述

在此处输入图像描述

标签: rshiny

解决方案


我只是移动了一个括号),以便您的第三个conditionalPanel嵌套在您的第二个中。这会给你想要的行为吗?

ui <- bootstrapPage(
  navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
             "Cl", 
             tabPanel("Solution",
                      fileInput("data", h3("Import excel")), 
                      sidebarLayout(
                        sidebarPanel(
                          
                          conditionalPanel(
                            "output.fileUploaded == true",
                            tags$hr(style="border-color: black;"),
                            tags$p(h3("Are you satisfied?")),
                            radioButtons( "satisfaction","", choices = list("Yes" = 1,"No " = 2),selected = 1)),
                          
                          conditionalPanel(
                            "input.satisfaction == '2'",
                            selectInput("naosatisf", h4("Choose a option below:"), 
                                        choices = c("no option selected" = "","Change the filters" = 1, "Change the cluster number" = 2), selected = ""),
                          
                          conditionalPanel(
                            "input.naosatisf == '2'",  
                            sliderInput("Slider", h5(""),
                                        min = 2, max = 31, value = "")),
                          
                        )),
                        mainPanel(
                          tabsetPanel(      
                            tabPanel("Solution", plotOutput("ScatterPlot"))))
                      ))))

推荐阅读