【发布时间】:2019-02-04 17:11:55
【问题描述】:
在我的 Shiny 应用程序中,我使用 for 循环在每次迭代时制作两个不同的图,我希望用户能够单独单击每个图。
count 变量记录按钮被点击的次数,但是,每次点击时,都会制作两个图,并且只显示最后渲染的图。
如何使用操作按钮显示每个情节?
library(shiny)
server <- function(input, output, session) {
# data
v <- c(9,8,7,8,9,5,6,7,4,3)
w <- c(3,4,2,3,3,3,2,3,4,5)
x <- c(1,3,4,6,2,4,6,8,6,3)
y <- c(4,5,2,4,2,1,2,5,7,8)
z <- c(5,9,8,6,4,6,8,9,6,7)
df <- data.frame(v, w, x, y, z)
# initial plot that will allow user to change parameters (haven't implemented yet)
output$plot <- renderPlot(plot(df[[1]],df[[2]]))
count<-0 # This is the counter which keeps track on button count
observeEvent(input$run, {
count <<- count + 1 # Increment the counter by 1 when button is click
if(count<6){
# Draw the plot if count is less than 6
output$plot <- renderPlot(plot(df[[1]],df[[count]],main = count))
output$plot <- renderPlot(plot(df[[3]],df[[count]],main = count))
}
else{
# Reset the counter if it is more than 5
count <- 0
}
})
}
ui <- fluidPage(
actionButton("run", "Generate"),
plotOutput("plot")
)
shinyApp(ui = ui, server = server)
【问题讨论】:
-
你可以尝试使用不同的
outputIds(output$plot1和output$plot2) -
会在应用程序的同一位置一次显示一个图吗? :)