Using renderPlot
或者里面的其他渲染函数observeEvent
被认为是不好的做法。您正在寻找的是一个代表绘图参数的反应对象。然后你可以在你的内部访问这个反应对象renderPlot
语境。这是一个例子
library(shiny)
ui <- shinyUI(fluidPage(
titlePanel("Iris Dataset"),
sidebarLayout(
sidebarPanel(
radioButtons("x", "Select X-axis:",
list("Sepal.Length" = 'a', "Sepal.Width" = 'b',
"Petal.Length" = 'c', "Petal.Width" = 'd')),
radioButtons("y", "Select Y-axis:",
list("Sepal.Length" = 'e', "Sepal.Width" = 'f',
"Petal.Length" = 'g', "Petal.Width" = 'h')),
actionButton("action", "Submit")
),
mainPanel(
plotOutput("distPlot")
)
)
))
server <- shinyServer(function(input, output) {
user_choice <- eventReactive(input$action,{
if (input$x == 'a'){i <- 1}
if (input$x == 'b'){i <- 2}
if (input$x == 'c'){i <- 3}
if (input$x == 'd'){i <- 4}
if (input$y == 'e'){j <- 1}
if (input$y == 'f'){j <- 2}
if (input$y == 'g'){j <- 3}
if (input$y == 'h'){j <- 4}
return(c(i, j))
## use ignoreNULL to fire the event at startup
}, ignoreNULL = FALSE)
output$distPlot <- renderPlot({
uc <- user_choice()
plot(iris[, uc[1]], iris[, uc[2]])
})
})
shinyApp(ui = ui, server = server)
物体user_choice
每当用户单击按钮时就会更新or应用程序启动。
还有一个ignoreNULL
参数输入observeEvent
但由于某种原因,这不会产生相同的结果。