0

I've got a really weird problem here with R and Shinyapps. The thing is that when I run the app in RStudio, everything works just fine but, when I publish it on Shinyapps.io I get the following error on some "textOutput" objects:

Error: invalid 'times' argument

It makes no sense to me but hey, there might be a reason!

I'm sharing my complete code here. It is a "half-way calculator" using ggmap, dplyr and shinyapp libraries on R.

For the ui.R file I have:

library(shiny)

shinyUI(fluidPage(
  titlePanel(""),
  sidebarLayout(
    sidebarPanel(h2("Menu"),
                 br(),
                 fluidRow(column(12,
                                 textInput("from", label = h4("From"),
                                           value = "Unicentro, Bogotá"))),
                 fluidRow(column(12,
                                 textInput("to", label = h4("To"),
                                           value = "Calle 116 #20-50, Bogotá"))),
                 fluidRow(
                   column(6,
                          selectInput("mode", label = h5("Mode"),
                                      choices = list("Walking" = "walking", 
                                                     "Driving" = "driving",
                                                     "Transit" = "transit",
                                                     "Bike" = "bicycling"
                                      ), selected = 1)),
                   column(6,
                          numericInput("zoom",
                                       label = h5("Zoom"),
                                       value = 15))),
                 br(),
                 fluidRow(column(12,submitButton("Calculate Halfway Point"),align="center"))
                 ),

    mainPanel(h1("Halfway Point Map"),
              p("NOTE: Wait a couple of second while we make the calculations and plot your map..."),
              p("For correct result, the ZOOM scale must be accurate (contain both, the from and to places)"),
              plotOutput("map",width=530, height=500),
              h3("Halfway Exact Address"),
              textOutput("dir"),
              h4("Details of trayectory"),
              tableOutput("tray")
    )
  )
))

For the server.R file:

library(ggmap)
library(dplyr)

shinyServer(function(input, output) {

  output$map <- renderPlot({
    ggmap(get_map(geocode(c(input$from,input$to)), zoom = input$zoom, maptype='roadmap', source='google', color='color'))+
      geom_segment(
        aes(x = startLon, y = startLat, xend = endLon, yend = endLat),
        colour ="red", size = 1, data = route(input$from, input$to, mode=input$mode)) + 
      geom_point(aes(x = halfLon, y = halfLat),
                 data = route(input$from, input$to, mode=input$mode) %>% mutate(cum.m = cumsum(m)) %>% 
                   filter(cum.m >= sum(route(input$from, input$to, mode=input$mode)[1])/2) %>% 
                   top_n(-1,cum.m) %>% 
                   mutate(halfLon = startLon+(endLon-startLon)*(1-(cum.m-sum(route(input$from, input$to, mode=input$mode)[1])/2)/m), 
                          halfLat = startLat+(endLat-startLat)*(1-(cum.m-sum(route(input$from, input$to, mode=input$mode)[1])/2)/m)) %>% 
                   select(halfLon,halfLat), 
                 colour = "black", size = 3)
  })

  output$dir <- renderText({
    paste(
      revgeocode(c(
        as.numeric(route(input$from, input$to, mode=input$mode) %>% mutate(cum.m = cumsum(m)) %>% 
                     filter(cum.m >= sum(route(input$from, input$to, mode=input$mode)[1])/2) %>% 
                     top_n(-1,cum.m) %>% 
                     mutate(halfLon = startLon+(endLon-startLon)*(1-(cum.m-sum(route(input$from, input$to, mode=input$mode)[1])/2)/m), 
                            halfLat = startLat+(endLat-startLat)*(1-(cum.m-sum(route(input$from, input$to, mode=input$mode)[1])/2)/m)) %>% 
                     select(halfLon)), 
        as.numeric(route(input$from, input$to, mode=input$mode) %>% mutate(cum.m = cumsum(m)) %>% 
                     filter(cum.m >= sum(route(input$from, input$to, mode=input$mode)[1])/2) %>% 
                     top_n(-1,cum.m) %>% 
                     mutate(halfLon = startLon+(endLon-startLon)*(1-(cum.m-sum(route(input$from, input$to, mode=input$mode)[1])/2)/m), 
                            halfLat = startLat+(endLat-startLat)*(1-(cum.m-sum(route(input$from, input$to, mode=input$mode)[1])/2)/m)) %>% 
                     select(halfLat))), 
        output="address"),
      " | ",
      sum(route(input$from, input$to, mode=input$mode)[1])/2,
      "m", sep="")
  })

  output$tray <- renderTable({
    route(input$from, input$to, mode=input$mode) %>% 
      select(-startLon,-startLat,-endLon,-endLat) %>%
      mutate(cum.m = cumsum(m))
  })

})

I'm attaching a couple of screenshot (right (R) and left (Shinyapps.io))

Link: https://laresbernardo.shinyapps.io/halfway_point_map/

Hope you can help me on this because it's getting me nuts! Thanks.

Bernardo
  • 461
  • 7
  • 20
  • I was checking your application and you seem to have fixed it. Please share the solution. – 67342343 Mar 28 '17 at 19:27
  • You should put the repeated code in a few reactives – HubertL Mar 28 '17 at 19:33
  • This error usually occurs in a `rep` call – HubertL Mar 28 '17 at 20:11
  • @67342343 I haven't fixed it.. check the image below. @HubertL I really haven't worked much with Shiny but I got some troubles defining objects inside the function or reactives. Do you know where can I see some examples to see if I can "clean up" some code? And about the rep error, where is the `rep` call on my code? Image: https://i.stack.imgur.com/hQxGa.png – Bernardo Mar 29 '17 at 20:31

0 Answers0