如果您想控制部分代码,我在线阅读有关使用操作按钮的信息。但是我在哪里放置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
答案 0 :(得分:1)
您可以这样做:
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)