【发布时间】:2021-06-02 13:13:38
【问题描述】:
我正在尝试制作具有以下特征的东西:
- 开始时不显示任何内容(无绘图输出),并且
- 仅在单击按钮后更新(在更改第一个或第二个 var 时不会更新)
到目前为止,我已经使用了 observeEvent 和 eventReactive 但两者都还没有工作。对于这个问题,我以 iris 为例。在过去的几个小时里,我一直在绞尽脑汁,我使用了许多不同的技术,例如 observeEvent、eventReactive、isolate() 等。
代码:
library(shiny)
library(datasets)
library(ggplot2)
data(iris)
ui <- shinyUI(pageWithSidebar(
headerPanel("plot example"),
sidebarPanel(
selectInput("var1", "First var",
list("Sepal length" = "Sepal.Length",
"Sepal width" = "Sepal.Width",
"Petal length" = "Petal.Length",
"Petal width" = "Petal.Width")),
selectInput("var2", "Second var",
list("Petal length" = "Petal.Length",
"Petal width" = "Petal.Width",
"Sepal length" = "Sepal.Length",
"Sepal width" = "Sepal.Width")),
actionButton("gobutton", "Go")
),
mainPanel(
plotlyOutput("plot"))
))
server <- shinyServer(function(input, output, session){
# I want the plot only to update if I press "Go"
# observe event only does this the first time
# observeEvent(eventExpr = {
# input$gobutton
# },
# handlerExpr = {
# output$plot <- renderPlotly({
# iris_plot <- ggplot(iris, aes_string(x=input$var1,
# y=input$var2,
# colour="Species")) + geom_point()
# ggplotly(iris_plot)
# })
# })
# this gives the error: Unknown input: reactive.event
inputVar <- eventReactive(input$gobutton, {
runif(input$var1, input$var2)
})
output$plot <- renderPlotly({
iris_plot <- ggplot(iris, aes_string(x=inputVar,
y=inputVar,
colour="Species")) + geom_point()
ggplotly(iris_plot)
})
# this below is the regular code
# output$plot <- renderPlotly({
# iris_plot <- ggplot(iris, aes_string(x=input$var1,
# y=input$var2,
# colour="Species")) + geom_point()
# ggplotly(iris_plot)
# })
})
shinyApp(ui = ui, server = server)
【问题讨论】: