سؤال

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)
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top