r - 使用绘图按钮后的图形输出
问题描述
如果您想控制代码的某些部分,我在网上阅读了有关使用操作按钮的信息。但是我在哪里放置 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
解决方案
你可以这样做:
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)
推荐阅读
- django - React 和 Django 如何真正协同工作
- c - Windows10平台_execl()函数的使用
- google-bigquery - BigQuery 中是否有一种方法可以保存来自区域内多区域的查询
- java - Android Exif 数据未存储在图片文件中
- asp.net-core - ConfigureWebHostDefaults 中的 UseSerilog 未知引用
- java - 在 java/jpa/hibernate 中不读取从其他外部程序修改的数据库表
- azure-data-factory - 数据工厂无法使用基本身份验证从 Web API 下载 CSV 文件
- python - Python - 找出图像的纵横比
- android - 在应用程序中重用 Room 或 SQLite 数据库?
- node.js - nodejs 与 supertest,为不同的测试用例调用一个端点两次得到一个你忘记在测试中等待异步的东西吗?