【问题标题】:Action Button and Isolate seem to not work as expected in ShinyAction Button 和 Isolate 在 Shiny 中似乎无法正常工作
【发布时间】:2015-04-25 03:19:04
【问题描述】:

我有一个闪亮的应用程序,可以根据用户的选择制作情节。因为我只想在选择操作按钮后绘制一个新图,所以我将绘图代码放在“隔离”中。结果是,只要按下操作按钮,就会出现一个新的绘图,但有时它也会在不按下按钮的情况下出现。特别是当我选择通过条件面板(“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)  
        }) 
      })
    })
} 

【问题讨论】:

  • 您的代码对我来说运行良好,并且按钮运行正常。检查您的反应式表达式(您说您有反应式的日期)并将 input$okButton 也放在那里(或将您的反应式日期放入您拥有的隔离中),因此它只会在按下时重新评估表达式。跨度>
  • 感谢您的测试和评论。我再次测试了这段代码,但不需要的功能仍然存在。很快,如果我绘制任何图表,而不是将颜色更改为“黄色选择”或“蓝色选择”并选择多个子类别之一(希腊字母),则情节也会发生变化,而无需按下操作按钮。这种情况有时会发生,但您应该能够在此复制游戏。
  • 我现在正在玩它 5 分钟,它对我来说很好用。 (我在闪亮的版本 0.11.1 和 R 3.1.2)。
  • 我刚刚将闪亮版本更新为 0.11.1,现在它似乎工作正常(我使用的是 10.2.2 版本)。感谢您的支持!
  • 现在我在 dateRangeInput 上遇到了一个不同的问题,因为如果时间范围是正数,则没有更多控制权(我可以选择比开始日早的最后一天,但这可能会导致一些问题)。

标签: r button plot shiny


【解决方案1】:

所以回到你的整体问题:

1) 您需要将 Shiny 更新到最新版本,这样才能解决按钮问题

2) 滑块输入现在同步,时间将设置为 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()

【讨论】:

  • 谢谢。这样它就可以正常工作。令我惊讶的是,这个控件是在以前版本的 Shiny 中实现的,但不是在最后一个版本中实现的。
猜你喜欢
  • 2019-12-12
  • 1970-01-01
  • 1970-01-01
  • 2017-09-04
  • 2012-05-06
  • 2013-02-17
  • 2017-10-09
  • 2017-08-03
  • 2014-05-02
相关资源
最近更新 更多