简体   繁体   中英

RStudio Shiny Conditional Plot

Im trying to create a web application with the new RStudio feature Shiny, which plots different stocks. I created the following example.

I want to select the dataset StockMarket then select eg the DAX and then finally the plot should appear.

Right now if you run this code the plot appears immediately

Could you help me please?

ui.R:
library(shiny)
library(ggplot2)

shinyUI(pageWithSidebar(

# Application title
headerPanel("Plot1"),
sidebarPanel(
selectInput("dataset", "Dataset", list("mtcars"="cars", "StockMarket"="stocks")),

conditionalPanel(
  condition = "input.dataset=='stocks'",
  uiOutput("data")
)),
mainPanel(
plotOutput('plotstock')) ))


server.R:
library(shiny)
require(ggplot2)
library(datasets)

shinyServer(function(input, output) {

output$data<- reactiveUI(function() {

selectInput("data", "Choose Dataset", colnames(EuStockMarkets))
})

output$plotstock <- reactivePlot(function() {
data<-data.frame(EuStockMarkets)
p<- ggplot(data,aes(x=seq(1,length(data[,1])),y=DAX))+geom_line(size=1)+ylab("")+opts(title="Time Series")
print(p)
 })})

In the reactivePlot function you can do something like

if (is.null(input$data))
  return(NULL)

I would also add a blank entry to the dataset choices ( "(Choose one)"="" ) and also have

if (!nzchar(input$dataset))
  return(NULL)

in reactivePlot .

Also make sure that you check for empty strings

if (!nzchar(input$dataset) || input$dataset=='')
return(NULL)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM