如何在Shiny上正确使用ggplotly

时间:2017-01-19 18:05:02

标签: r ggplot2 shiny leaflet

我的问题是:如何在Shiny上正确使用ggplotly?

代码工作正常,最后没有“ggplotly()”行。我不知道为什么Shiny没有制作图形。

library(shiny)
library(leaflet)
library(ggplot2)


ui <- fluidPage(
  leafletOutput("mymap"),
  p(),
  vars <- c(
    "Las Cruzadas" = "cruzadas"
  ),

  absolutePanel(id = "controls", class = "panel panel-default", fixed = TRUE,
    draggable = TRUE, top = "auto", left = "auto", right = "auto", bottom = 60,
    width = 900, height = "auto",

  h2("Serie nivel napa"),

  selectInput("color", "Estacion", vars),

  plotOutput("variacion", height = 100)


  )
)
server <- function(input, output, session) {

  output$mymap <- renderLeaflet({
    leaflet() %>%
      addTiles() %>%
      addCircleMarkers(lng=-71.294444, lat=-32.933333, color="blue" ,popup="ESTACION: 'LAS CRUZADAS'")
  })
  output$variacion <- renderPlot({
    setwd("C:/Documents and Settings/waldo.solar.g/Escritorio")
    datos=read.csv("cruzadas.csv", sep=";", header = TRUE)
    names(datos) <- c("fecha","hora","altura")
    datos$fecha <- gsub("[[:punct:]]","/",datos$fecha)
    datos$hora <- gsub(":00",":00:00",datos$hora)
    datos$altura[ datos$altura == "---" ] = NA
    datos$altura2 <-as.numeric(as.character(datos[,3])) * -1
    datos$horafecha <- strptime(paste(datos$fecha, datos$hora), format = "%d/%m/%Y %H:%M:%S")
    theme_set(theme_bw()) 
    ggplot(aes(x = datos$horafecha, y = datos$altura2), data = datos) + 
      geom_line() +
      geom_point() +
      #geom_point(aes(y = datos$altura2, color = "Linear"), size = 3, alpha = 0.5) + 
      stat_smooth(colour='blue', span=0.2) +
      ylab("Altura (m) ") + 
      xlab("Meses")

    ggplotly() #THIS LINE CONTAINS THE ERROR. HELP! :)
  })
}  

shinyApp(ui, server)

1 个答案:

答案 0 :(得分:2)

我明白了!

在UI中

plotlyOutput("variacion", height = "80%")

并在服务器中

output$variacion <- renderPlotly({