简体   繁体   English

RStudio Shiny Conditional Plot

[英]RStudio Shiny Conditional Plot

Im trying to create a web application with the new RStudio feature Shiny, which plots different stocks. 我试图用新的RStudio功能Shiny创建一个Web应用程序,它绘制了不同的股票。 I created the following example. 我创建了以下示例。

I want to select the dataset StockMarket then select eg the DAX and then finally the plot should appear. 我想选择数据集StockMarket然后选择例如DAX,然后最终应该出现该图。

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 reactivePlot函数中,您可以执行类似的操作

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

I would also add a blank entry to the dataset choices ( "(Choose one)"="" ) and also have 我还要在数据集选项中添加一个空白条目( "(Choose one)"="" )并且还有

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

in reactivePlot . reactivePlot

Also make sure that you check for empty strings 还要确保检查空字符串

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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