动作按钮和隔离似乎在Shiny中无法正常工作

时间:2015-02-23 17:41:58

标签: r button plot shiny

我有一个闪亮的应用程序根据用户选择制作情节。因为我只想在选择动作按钮后选择一个新的情节,所以我将绘图代码放在"隔离"中。结果是,一旦按下操作按钮,就会出现一个新的情节,但有时它也会在没有按下按钮的情况下出现。特别是当我选择通过条件面板显示的子类别参数时(" Alpha"," Beta"," Gamma",...)。在原始版本中,更改日期也可能导致绘图重新执行。这是我的代码:我错过了什么?

callingColorPlot <- function(var) {
  listaMia<- list("alpha" = 2, "beta" = 3, "gamma" = 4, "delta" = 5, "epsilon" = 6, "zeta" = 7, "eta" = 8)
  valori <- (c(1:8))*10
  require(shiny)
  shinyApp(
    ui = fluidPage(
      titlePanel("Title"),
      sidebarLayout(
        sidebarPanel (position = "left", 
                      helpText(h4("Specificy:")), br(),       
                      selectInput("selectedColor", label = (strong("Select your color")), 
                                  choices = list( "Yellow selection", "Orange selection", "Blue selection", "Gray selection"), selected = ""),
                      conditionalPanel (
                        condition = "input.selectedColor == 'Yellow selection'",
                        selectInput("subCategory", label = (strong("Select sub categories")), choices = listaMia, selected="")
                      ),  
                      conditionalPanel (
                        condition = "input.selectedColor == 'Blue selection'",
                        checkboxGroupInput("letterCheckbox", label = strong("Select your letter"), 
                                           choices = listaMia, selected = "")
                      ), 
                      br(),
                      dateRangeInput("dates", label = (strong("Time Interval")), start='2012-01-02', end='2012-10-31', min='2012-01-02', max='2012-10-31'), 
                      sliderInput("sliderBegin", label = strong("Starting hour"), min = 0, max = 23, value = 0),
                      uiOutput("sliderInput"),
                      br(), actionButton("okButton", "Go")
        ),
        mainPanel(plotOutput("plotResult")))  
    ), 
    server = function(input, output, session) 
      {
      output$sliderInput <- renderUI ({
        sliderInput("sliderFinal", label = strong("Final hour"), min = 0, max = 24, value = 24);           
      })     
      output$plotResult <- renderPlot ( {
        input$okButton
        if (input$okButton == 0)
        return()              
         isolate ({ 
           plotWidth=c(430, 430, 430, 430, 430, 430, 430, 430)
           plotNames=c("1", "2", "3", "4", "5", "6", "7", "Tot")
          if (input$selectedColor == 'Yellow selection') { 
            color='yellow'
            xlabel="Yellow selection"
            mainTitle="Yellow selection"
          }else if (input$selectedColor == 'Orange selection') {
            color='orange'
            xlabel="Orange selection"
            mainTitle="Orange selection"  
          }else if (input$selectedColor == 'Blue selection') {              
            if (length(input$letterCheckbox)==0)
              return()
            color='darkblue'
            xlabel="Blue selection"
            mainTitle="Blue selection"
          }else if (input$selectedColor == 'Gray selection') {      
            color='darkgray'
            xlabel="Gray selection"
            mainTitle="Gray selection"
          } 
          mychart <- barplot (valori, col = color, border='white'
                              , width=plotWidth, space=0.2
                              , xlab=xlabel, ylab="values", names.arg=plotNames
                              , ylim=c(0, 100), xlim=c(150, 3750), main=mainTitle)  
        }) 
      })
    })
} 

1 个答案:

答案 0 :(得分:0)

总体上回到你的问题:

1)您需要将Shiny更新为最新版本,以便通过按钮解决问题

2)Sliderinputs现在同步,时间将设置为24小时

3)我添加了观察声明来控制将控制负日期范围的DateRangeInput

callingColorPlot <- function(var) {
  listaMia<- list("alpha" = 2, "beta" = 3, "gamma" = 4, "delta" = 5, "epsilon" = 6, "zeta" = 7, "eta" = 8)
  valori <- (c(1:8))*10
  require(shiny)
  shinyApp(
    ui = fluidPage(
      titlePanel("Title"),
      sidebarLayout(
        sidebarPanel (position = "left", 
                      helpText(h4("Specificy:")), br(),       
                      selectInput("selectedColor", label = (strong("Select your color")), 
                                  choices = list( "Yellow selection", "Orange selection", "Blue selection", "Gray selection"), selected = ""),
                      conditionalPanel (
                        condition = "input.selectedColor == 'Yellow selection'",
                        selectInput("subCategory", label = (strong("Select sub categories")), choices = listaMia, selected="")
                      ),  
                      conditionalPanel (
                        condition = "input.selectedColor == 'Blue selection'",
                        checkboxGroupInput("letterCheckbox", label = strong("Select your letter"), 
                                           choices = listaMia, selected = "")
                      ), 
                      br(),
                      dateRangeInput("dates", label = (strong("Time Interval")), start='2012-01-02', end='2012-10-31', min='2012-01-02', max='2012-10-31'), 
                      sliderInput("sliderBegin", label = strong("Starting hour"), min = 0, max = 23, value = 0),
                      uiOutput("sliderInput"),
                      br(), actionButton("okButton", "Go")
        ),
        mainPanel(plotOutput("plotResult")))  
    ), 
    server = function(input, output, session) 
    {

      observe({
        if(input$dates[1] > input$dates[2])
        {
          date1 <- input$dates[1]
          updateDateRangeInput(session, "dates", label = (paste("Time Interval")), start=date1, end=date1, min='2012-01-02', max='2012-10-31') 
        }
      })

      output$sliderInput <- renderUI ({
        sliderInput("sliderFinal", label = strong("Final hour"), min = input$sliderBegin, max = 24, value = 24);           
      })    
      output$plotResult <- renderPlot ( {
        input$okButton
        if (input$okButton == 0)
          return()              
        isolate ({ 
          plotWidth=c(430, 430, 430, 430, 430, 430, 430, 430)
          plotNames=c("1", "2", "3", "4", "5", "6", "7", "Tot")
          if (input$selectedColor == 'Yellow selection') { 
            color='yellow'
            xlabel="Yellow selection"
            mainTitle="Yellow selection"
          }else if (input$selectedColor == 'Orange selection') {
            color='orange'
            xlabel="Orange selection"
            mainTitle="Orange selection"  
          }else if (input$selectedColor == 'Blue selection') {              
            if (length(input$letterCheckbox)==0)
              return()
            color='darkblue'
            xlabel="Blue selection"
            mainTitle="Blue selection"
          }else if (input$selectedColor == 'Gray selection') {      
            color='darkgray'
            xlabel="Gray selection"
            mainTitle="Gray selection"
          } 
          mychart <- barplot (valori, col = color, border='white'
                              , width=plotWidth, space=0.2
                              , xlab=xlabel, ylab="values", names.arg=plotNames
                              , ylim=c(0, 100), xlim=c(150, 3750), main=mainTitle)  
        }) 
      })
    })
} 

callingColorPlot()