r-根据2个输入用动作按钮绘制



我正在尝试制作具有以下特性的东西:

  • 它在开始时不显示任何内容(没有绘图输出(,并且
  • 它只在单击按钮后更新(而不是在更改第一个或第二个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)

这应该能在中工作

rv <- reactiveValues(var1=NULL,var2=NULL)
observeEvent(input$gobutton,{
rv$var1 <- input$var1
rv$var2 <- input$var2
})
output$plot <- renderPlotly({
if (!is.null(rv$var1) & !is.null(rv$var2)){
iris_plot <- ggplot(iris, aes_string(x=rv$var1, 
y=rv$var2,
colour="Species")) + geom_point()
ggplotly(iris_plot)
}
})

req(输入$gobutton(应该完成

output$plot <- renderPlotly({
req(input$gobutton)
iris_plot <- ggplot(iris, aes_string(x=input$var1,
y=input$var2,
colour="Species")) + geom_point()
ggplotly(iris_plot)

相关内容

  • 没有找到相关文章

最新更新