【发布时间】:2017-03-20 07:41:31
【问题描述】:
我花了最后半个小时阅读有关 conditionalPanel 以及 actionButtons 以及每次按下按钮时它们的值如何递增 1 的内容。话虽如此,这就是我在下面尝试做的事情以及我面临的问题:
问题 我正在使用 conditionalPanel 在两个不同的渲染图之间切换。我使用两个不同的渲染图而不是同一个渲染图中的两个图的原因是,两个图具有不同的高度/宽度尺寸很重要。我在 conditionalPanel 中使用 actionButton 的值,这很糟糕。
我想要什么 如果最近按下了 2 个按钮,我希望显示移动图表。如果最近按下了 toggleshotchart,我希望显示 shot.chart。
我希望我能做什么 如果我只能在条件中使用 plotData$value,我就准备好了。 plotData$value 用于在 renderPlots 中输入 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)
我阅读了很多关于重置 actionButton 值的内容,但找不到任何可以解决我的问题的内容。似乎将 actionButton 值重置为 0 并不容易/无法完成。对我的这项任务的任何想法将不胜感激!
编辑 - 我之前问过这个相关问题 - In RShiny, change plot width / height for separate plots within same renderPlot() - 解决方案在哪里有效,但问题略有不同。我在这个问题上工作得越多,我就越意识到我最初的问题并没有解决我的实际问题。
【问题讨论】: