Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/91.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在RShiny中,在conditionalPanel中使用actionButton值会引起很多麻烦_R_Shiny - Fatal编程技术网

在RShiny中,在conditionalPanel中使用actionButton值会引起很多麻烦

在RShiny中,在conditionalPanel中使用actionButton值会引起很多麻烦,r,shiny,R,Shiny,我花了半个小时阅读了conditionalPanel和actionButtons,以及它们的值在每次按下按钮时是如何增加1的。话虽如此,以下是我试图做的事情以及我面临的问题: 问题 我使用conditionalPanel在两个不同的渲染图之间切换。我之所以使用两个不同的渲染图,而不是同一渲染图中的两个图,是因为两个图具有不同的高度/宽度尺寸非常重要。我在conditionalPanel中使用actionButton的值,这很糟糕 我想要什么 如果最近按下了两个按钮中的togglemovement

我花了半个小时阅读了conditionalPanel和actionButtons,以及它们的值在每次按下按钮时是如何增加1的。话虽如此,以下是我试图做的事情以及我面临的问题:

问题 我使用conditionalPanel在两个不同的渲染图之间切换。我之所以使用两个不同的渲染图,而不是同一渲染图中的两个图,是因为两个图具有不同的高度/宽度尺寸非常重要。我在conditionalPanel中使用actionButton的值,这很糟糕

我想要什么 如果最近按下了两个按钮中的togglemovement actionButton,我希望显示movement.chart。如果最近按下了toggleshotchart,我希望shot.chart显示

我希望我能做的事 如果我只能在条件中使用plotData$值,我将被全部设置。plotData$值用于在RenderPlot中输入if语句,以确定应打印哪些绘图,但我不能在conditionalPanel中使用它们

说到这里,这里是我的项目的一个简短版本

library(shiny)

# 2. UI layout
# ============
ui <- fluidPage(
        fluidRow(
          column(width = 4, align = 'center', 
              actionButton(inputId = 'toggleshotchart', label = 'Launch Shots'), 
              actionButton(inputId = 'togglemovement', label = 'Launch Movements')
          ),

          # This displays either the shot or movement chart
          # ===============================================
          column(width = 8, align = 'left',
                 conditionalPanel("input.togglemovement > input.toggleshotchart",
                                  plotOutput('movement.chart', height = 650, width = 1128)
                 ),
                 conditionalPanel("input.togglemovement <= input.toggleshotchart",
                                  plotOutput('shot.chart', height = 846, width = 900)
                 )
          )
        )
)

# 3. Server Layout
# ================
server <- shinyServer(function(input, output) {

  # Create some reactive stuff to toggle charts
  plotData <- reactiveValues(value = NULL)

  observeEvent(input$toggleshotchart, {
    plotData$value <- 0
  })

  observeEvent(input$togglemovement, {
    plotData$value <- 1
  })

  # create the first chart
  output$shot.chart <- renderPlot({

    # this chart is displayed at launch
    if (is.null(plotData$value)) {
      plot(c(1,2,3,4,5), c(1,2,3,4,5))
    }

    # this chart SHOULD BE displayed after clicking toggleshotchart
    else if(plotData$value == 0) {
      hist(rnorm(10))
    }      

    # Do nothing (prev displayed motion chart here)
    else {
      # do nothing
    }
  })

  # this chart SHOULD BE displayed after clicking togglemovementchart
  output$movement.chart <- renderPlot({
    if(plotData$value == 1) {
      hist(c(1,2,3,2,1))
    }
  })
})

shinyApp(ui = ui, server = server)
库(闪亮)
# 2. 用户界面布局
# ============

ui使用
单选按钮在两个绘图之间切换怎么样

library(shiny)

# 2. UI layout
# ============
ui <- fluidPage(
  fluidRow(
    column(width = 4, 
           radioButtons("choice_plot","Launch",choices=list("Shots","Movements"), selected="Shots")),

    # This displays either the shot or movement chart
    # ===============================================
    column(width = 8, align = 'left', uiOutput("plot_ui"))
  )
)

# 3. Server Layout
# ================
server <- shinyServer(function(input, output) {

  output$plot_ui <- renderUI({
    if(input$choice_plot == 'Shots') {
      plot.width = 1128
      plot.height = 650
    }else{
      plot.width = 900
      plot.height = 846
    }
    plotOutput('plot', width = plot.width, height = plot.height)

  })

  output$plot <- renderPlot({
    if(input$choice_plot == 'Shots'){
      hist(rnorm(10))
    }else{
      hist(c(1,2,3,2,1))
      }
  })

})

shinyApp(ui = ui, server = server)

使用
单选按钮
在两个绘图之间切换怎么样

library(shiny)

# 2. UI layout
# ============
ui <- fluidPage(
  fluidRow(
    column(width = 4, 
           radioButtons("choice_plot","Launch",choices=list("Shots","Movements"), selected="Shots")),

    # This displays either the shot or movement chart
    # ===============================================
    column(width = 8, align = 'left', uiOutput("plot_ui"))
  )
)

# 3. Server Layout
# ================
server <- shinyServer(function(input, output) {

  output$plot_ui <- renderUI({
    if(input$choice_plot == 'Shots') {
      plot.width = 1128
      plot.height = 650
    }else{
      plot.width = 900
      plot.height = 846
    }
    plotOutput('plot', width = plot.width, height = plot.height)

  })

  output$plot <- renderPlot({
    if(input$choice_plot == 'Shots'){
      hist(rnorm(10))
    }else{
      hist(c(1,2,3,2,1))
      }
  })

})

shinyApp(ui = ui, server = server)

嗨,马尔维纳,谢谢你对这篇文章和我的另一篇文章的评论。在我的完整应用程序中,有几个selectInput切换也可以更改每个绘图的功能。在更改selectInput切换后,会单击操作按钮以更新图表,这就是为什么我强烈希望保留操作按钮而不是单选按钮。如果我无法使操作按钮按需要工作,那么,我想这是一个不错的选择。你能解释一下吗?我只使用了
actionButton
来解决你的问题,看看answerHI Malvina,谢谢你对这篇文章以及我的其他帖子的评论。在我的完整应用程序中,有几个selectInput切换也可以更改每个绘图的功能。在更改selectInput切换后,会单击操作按钮以更新图表,这就是为什么我强烈希望保留操作按钮而不是单选按钮。如果我无法使操作按钮按需要工作,那么我想这将是一个不错的选择。你能解释一下吗?我已经试着只使用
actionButton
来解决你的问题,看看答案