Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
 })})
like image 953
rainer Avatar asked Nov 15 '12 18:11

rainer


1 Answers

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.

like image 111
Joe Cheng Avatar answered Nov 15 '22 16:11

Joe Cheng