目前,我正在學習如何在 Web 應用程式中可視化預測資料與原始資料。由于我想為原始資料和預測資料設定單獨的選項卡,因此我使用了導航欄面板。
在給定的 reprex 中,兩個資料幀都包含相同的資料,因為它僅用于技術說明。
目前我正在努力顯示代表預測圖的 plot2 資料。我猜這與錯誤的語法有關。否則它可能是一個 html 問題。
請在這里幫助我:
library(shiny)
library(ggplot2)
library(dplyr)
library(hrbrthemes)
library(shinythemes)
dates <- ymd("2016-01-01") months(0:59)
fake_values <-
c(661,678,1094,1987,3310,2105,1452,983,1107)
replicate <- rep(1,60) %*% t.default(fake_values)
replicate <- as.data.frame(replicate)
df <- bind_cols(replicate, dates) %>%
rename(c(dates = ...10))
## melt it down
data <- reshape2::melt(df, id.var='dates')
data$variable <- as.character(data$variable)
data$dates <- as.Date(data$dates)
data2 <- reshape2::melt(df, id.var='dates')
data2$variable <- as.character(data2$variable)
data2$dates <- as.Date(data2$dates)
#### UI
ui <-
navbarPage(
title="Zeitreihenvorhersage",
theme=shinytheme("spacelab"),
inverse=TRUE,
# first tab
tabPanel(
"Original Zeitreihe",
fluidPage(
sidebarPanel(
selectInput(
inputId = "variable",
label = "Zeitreihe selektieren",
choices = unique(data$variable),
selected = "V1")
),
mainPanel(
plotOutput("plot", click = "plot_click"),
verbatimTextOutput("info")
)
)
),
# second tab
tabPanel(
"Forecast",
fluidPage(
sidebarPanel(
selectInput(
inputId = "variable",
label = "Zeitreihe selektieren",
choices = unique(data2$variable),
selected = "V6")
),
mainPanel(
plotOutput("plot2", click = "plot_click2"),
verbatimTextOutput("info2")
)
)
)
)
#### SERVER
server <- function(input, output, session) {
output$plot <- renderPlot({
data %>%
filter(variable == input$variable) %>%
ggplot(aes(dates, value, group = 1))
geom_line( color="steelblue", size = 1.2)
geom_point(size = 2.5)
xlab("")
ylab("Absatzmenge")
scale_x_date(date_breaks = "2 months")
theme_bw()
theme(
panel.border = element_blank(),
axis.text.x = element_text(angle = 60, vjust = 1, hjust = 1)
)
}
)
output$info <- renderText({
paste0("x=", input$plot_click$x, "\ny=", input$plot_click$y)
}
)
#}
output$plot2 <- renderPlot({
data2 %>%
filter(variable == input$variable) %>%
ggplot(aes(dates, value, group = 1))
geom_line( color="steelblue", size = 1.2)
geom_point(size = 2.5)
xlab("")
ylab("Absatzmenge")
scale_x_date(date_breaks = "2 months")
theme_bw()
theme(
panel.border = element_blank(),
axis.text.x = element_text(angle = 60, vjust = 1, hjust = 1)
)
output$info2 <- renderText({
paste0("x=", input$plot_click2$x, "\ny=", input$plot_click2$y)
}
)
}
)
}
runApp(list(ui = ui, server = server),host="127.x.x.x",port=9999, launch.browser = TRUE)
uj5u.com熱心網友回復:
您錯過了renderPlot在渲染之前關閉第二個output$info2- 如您所見,在output$info2.
這是改進版:
output$plot2 <- renderPlot({
data2 %>%
filter(variable == input$variable) %>%
ggplot(aes(dates, value, group = 1))
geom_line( color="steelblue", size = 1.2)
geom_point(size = 2.5)
xlab("")
ylab("Absatzmenge")
scale_x_date(date_breaks = "2 months")
theme_bw()
theme(
panel.border = element_blank(),
axis.text.x = element_text(angle = 60, vjust = 1, hjust = 1)
)
})
output$info2 <- renderText({
paste0("x=", input$plot_click2$x, "\ny=", input$plot_click2$y)
}
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/371946.html
