Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R中的闪亮应用程序中无法获取图形_R_Ggplot2_Shiny - Fatal编程技术网

在R中的闪亮应用程序中无法获取图形

在R中的闪亮应用程序中无法获取图形,r,ggplot2,shiny,R,Ggplot2,Shiny,我正在尝试使用Shiny创建小型应用程序。下面是我试图创建的数据帧 data<-data.frame(state=c('AZ','VA','CA','AZ','VA','CA'), city=c('Phoenix','Arlington','SantaClara','Mesa','Richmond','SF'), avg=c(10,15,16,13,14,14), date=c('01/09/2017','01/10/2017','01/11

我正在尝试使用Shiny创建小型应用程序。下面是我试图创建的数据帧

data<-data.frame(state=c('AZ','VA','CA','AZ','VA','CA'), city=c('Phoenix','Arlington','SantaClara','Mesa','Richmond','SF'),
                        avg=c(10,15,16,13,14,14), date=c('01/09/2017','01/10/2017','01/11/2017','02/09/2017','02/10/2017','02/10/2017'),stringsAsFactors = FALSE)

data我对您的代码做了一些小修改:

  • 在它们不应该出现的地方有一些逗号:在ui构造函数之后和观察构造函数之后
  • data[data$city==input$plot2&
    数据$state==输入$plot1,]
  • 我把你的观察编辑成一个观察者
  • 我修改了图以显示它实际上发生了变化,因为样本数据非常有限
希望这有帮助


库(闪亮)
图书馆(GG2)
图书馆(绘本)

多谢弗洛里安。我想要y轴上的月/年和条形图或线形图。你能帮忙吗?这似乎是个新问题。请考虑将其作为堆栈溢出的新问题。
library(shiny)
library(ggplot2)
library(plotly)

statelist<-as.list(data$state)
citylist<-as.list(data$city)
ui <- basicPage(
  # plotOutput("plot1", click = "plot_click"),
  # verbatimTextOutput("info")
  sidebarPanel(
    selectInput("plot1", label=h3("Select State"), choices = statelist),
    selectInput("plot2", label=h3("Select City"), choices = citylist)
  ),
  plotOutput(outputId="plot")
),

server <- function(input, output, session) {
  observe(
    {
      state <- input$plot1
      updateSelectInput(session, "plot2", choices = data$city[data$state == state])
    }
  ),
  output$plot<-renderPlot({
    ggplot(data[data$city == input$plot2 & 
                  data$state == input$plot1],aes(date,avg))
    +geom_line()
  })
}

shinyApp(ui, server)
library(shiny)
library(ggplot2)
library(plotly)

data<-data.frame(state=c('AZ','VA','CA','AZ','VA','CA'), city=c('Phoenix','Arlington','SantaClara','Mesa','Richmond','SF'),
                 avg=c(10,15,16,13,14,14), Date=c('01/09/2017','01/10/2017','01/11/2017','02/09/2017','02/10/2017','02/10/2017'),stringsAsFactors = FALSE)

statelist<-unique(data$state)
citylist<-unique(data$city)
ui <- basicPage(
  # plotOutput("plot1", click = "plot_click"),
  # verbatimTextOutput("info")
  sidebarPanel(
    selectInput("plot1", label=h3("Select State"), choices = statelist),
    selectInput("plot2", label=h3("Select City"), choices = citylist)
  ),
  plotOutput(outputId="plot")
)

server <- function(input, output, session) {
  observeEvent(input$plot1,
    {
      state <- input$plot1
      updateSelectInput(session, "plot2", choices = data$city[data$state == state])
    }
  )

  output$plot<-renderPlot({
    data = data[data$city == input$plot2 & 
           data$state == input$plot1,]
        ggplot(data,aes(Date,avg)) + geom_point(size=5) + ggtitle(paste0(input$plot1," - ",input$plot2 ))

  })
}

shinyApp(ui, server)