在我的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)
答案 0 :(得分:0)
就像Gregor de Cillia所说,可以使用不同的地块。
编辑1:仅显示plot1
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)
# Make two different plots here
output$plot1 <- renderPlot(plot(df[[1]],df[[2]]))
output$plot2 <- renderPlot(plot(df[[3]],df[[1]]))
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
# Update both these plots
output$plot1 <- renderPlot(plot(df[[1]],df[[count]],main = count))
# Generate this plot but do not output
plot2 <- reactive({
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"),
# Output both plots separately
plotOutput("plot1")
# Don't show this
# plotOutput("plot2")
)
shinyApp(ui = ui, server = server)
编辑2:根据最新的理解,这是实现所需内容的方法:
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$plot1 <- 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 plot if count < 6
# In every iteration, first click is an odd number,
# for which we display the first plot
if(count %% 2 != 0){
# Draw first plot if count is odd
output$plot1 <- renderPlot(plot(df[[1]],df[[count]],main = count))
}
else{
# Second click is an even number,
# so we display the second plot in the previous iteration,
# hence df[[count - 1]]
# Draw second plot if count is even
output$plot1 <- renderPlot(plot(df[[3]],df[[count-1]],main = count))
}
}
else{
# Reset the counter if it is more than 5
count <- 0
}
})
}
ui <- fluidPage(
actionButton("run", "Generate"),
# Output plot
plotOutput("plot1")
)
shinyApp(ui = ui, server = server)