首页 > 解决方案 > 使用绘图按钮后的图形输出

问题描述

如果您想控制代码的某些部分,我在网上阅读了有关使用操作按钮的信息。但是我在哪里放置 observeEvent() 函数,以便在按下绘图按钮后绘制图形

 library(shiny)
library(ggplot2)
library(quantmod)

# Define UI for application that draws a bar graph
ui <- fluidPage(

  # Application title
  titlePanel("My Plot"),

  sidebarLayout(
    sidebarPanel(
      textInput("text", "Enter company name:", width = NULL,
                placeholder = "comp.name"),
      dateRangeInput("dates", h3(strong("Date Range")), 
                     start = "2001-01-01", end = Sys.Date(),
                     min = "0000-01-01", max = Sys.Date(),
                     format = "dd-mm-yy", separator = strong("to"),
                     autoclose = TRUE),
      submitButton(text = "submit"),
      actionButton("button", "PLOT")),

    # Show a plot
    mainPanel( h1(strong(textOutput("Company"))),
               DT::dataTableOutput("MRF"),
               plotOutput("finally")
    )
  )
)

# Define server logic
server <- function(input, output) {

  output$MRF <- DT::renderDataTable({
    tyu2 <- getSymbols(input$text , src = "yahoo", verbose = TRUE,
                       to = max(input$dates), from = min(input$dates) ,
                       auto.assign = FALSE)})
  output$finally <- renderPlot({
    tyu2 <- getSymbols(input$text , src = "yahoo", verbose = TRUE,
                       to = max(input$dates), from = min(input$dates) ,
                       auto.assign = FALSE, row.names = TRUE)
    ggplot(data = tyu2 , aes(x= min(input$dates):max(input$dates) ,
                             y= tyu2[2]))+
      geom_bar(stat = "identity", fill = "blue")+
      theme(axis.text.x = element_text(angle = 90)) +
      xlab("Dates")+ ylab(input$text)
   })
}

# Run the application 
shinyApp(ui = ui, server = server)

到目前为止,它从一开始就给出了这个错误。我希望它等到提交按钮和绘图按钮被按下

Aesthetics must be either length 1 or the same as the data (6378): x, y

标签: rshiny

解决方案


你可以这样做:

library(shiny)
library(ggplot2)
library(quantmod)

# Define UI for application that draws a bar graph
ui <- fluidPage(

  # Application title
  titlePanel("My Plot"),

  sidebarLayout(
    sidebarPanel(
      textInput("text", "Enter company name:", width = NULL,placeholder = "comp.name"),
      dateRangeInput("dates", h3(strong("Date Range")), start = "2001-01-01", end = Sys.Date(),min = "0000-01-01", max = Sys.Date(), format = "dd-mm-yy", separator = strong("to")),
      actionButton("submit", "submit"),
      actionButton("plot", "PLOT")),

    # Show a plot
    mainPanel(h1(strong(textOutput("Company"))),
               DT::dataTableOutput("MRF"),
               plotOutput("finally")
    )
  )
)

# Define server logic
server <- function(input, output) {

  mydata <- eventReactive(input$submit,{
    req(input$text)
    getSymbols(input$text , src = "yahoo", verbose = TRUE,
               to = max(input$dates), from = min(input$dates) ,
               auto.assign = FALSE)
  })

  output$MRF <- DT::renderDataTable({mydata()})

  myplotdata <- eventReactive(input$plot,{
    autoplot(mydata()[,2])+
      geom_bar(stat = "identity", fill = "blue")+
      theme(axis.text.x = element_text(angle = 90)) +
      xlab("Dates")+ ylab(input$text)
  })

  output$finally <- renderPlot({
    myplotdata()
  })
}

# Run the application 
shinyApp(ui = ui, server = server)

在此处输入图像描述


推荐阅读