我有以下代码。选择“ copen_cclose”后,“ pclose_chigh”图将消失。选择“ copen_cclose”后,是否可以保留图“ pclose_chigh”。我们可以保留地块吗?
library(quantmod)
require(quantmod)
library(gdata)
library(magrittr)
library(ggplot2)
library("gridExtra")
two_com <- c("3MINDIA.NS")
df1 <- as.data.frame(getSymbols(two_com, src = "yahoo", from = "2018-01-01", to = Sys.Date(), verbose = FALSE, auto.assign = TRUE))
allstocks <- fortify.zoo(`3MINDIA.NS`)
newcol <- c("Date","open","high","low","close","volume","adjusted")
colnames(allstocks) <- newcol
allstocks$MaxProfit <- round(allstocks$high - allstocks$low, digits = 1)
allstocks$day <- weekdays(allstocks$Date)
allstocks$month <- months(allstocks$Date)
allstocks$pclose_chigh1 <- round(c(NA, allstocks$close[-nrow(allstocks)]) - allstocks$high,digits = 1)*(-1)
allstocks$pclose_chigh1 <- as.numeric(as.character(allstocks$pclose_chigh1))
allstocks$copen_cclose <- round(allstocks$open-allstocks$close, digits = 1)
library(shiny)
ui <- fluidPage(
tabsetPanel(tabPanel("tab", sidebarLayout(
sidebarPanel(
h6(selectInput("plots", "plots", choices = c("Null", "pclose_chigh", "copen_cclose"))),
h6(selectInput("day", "Day", choices = c("Null", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"))),
em(verbatimTextOutput("ST"))
),
mainPanel(
h6(plotOutput("message3")),
h6(plotOutput("message2"))
)
)))
)
server <- function(input, output, session) {
output$message3 <- renderPlot({
if (input$plots == "pclose_chigh") {
qplot(pclose_chigh1, data = allstocks, geom = "density", colour = day)
}
})
output$message2 <- renderPlot({
if (input$plots == "copen_cclose") {
qplot(copen_cclose, data = allstocks, geom = "density", colour = day)
}
})
output$ST <- renderPrint({
if (input$day == "Monday") {
shapiro.test(allstocks$pclose_chigh1[allstocks$day=="Monday"])
}
})
}
shinyApp(ui, server)
Is there a function for solve this?
答案 0 :(得分:0)
您正在做的是每次input$plots
更改时都绘制两个图。由于您只有一个if
语句(并且没有else
语句),因此只有在满足条件的情况下才可以绘制图。如果不是,则不呈现任何内容,并删除旧图。
因此,您可以添加一个else
语句,或者在服务器中执行以下操作:
server <- function(input, output, session) {
observeEvent(input$plots, {
if (input$plots == "pclose_chigh") {
output$message3 <- renderPlot(qplot(pclose_chigh1, data = allstocks, geom = "density", colour = day))
} else if (input$plots == "copen_cclose") {
output$message2 <- renderPlot(qplot(copen_cclose, data = allstocks, geom = "density", colour = day))
}
})
output$ST <- renderPrint({
if (input$day == "Monday") {
shapiro.test(allstocks$pclose_chigh1[allstocks$day=="Monday"])
}
})
}