0

I am using shiny to create a simple app.

ui.R

library(shiny)
shinyUI(fluidPage(
  titlePanel("Power Breakdown!"),
  sidebarLayout(
    sidebarPanel()
      mainPanel(
        plotOutput("plt"))
      )
     ) ) 

and server.R is (needed dataset is at loc)

library(shiny)
library(ggplot2)
df <- readRDS("dataframe-path")
shinyServer(
  function(input, output) {
     output$plt <- renderPlot({
       df$timestamp <- as.Date(df$timestamp)
       p <- ggplot(df,aes(df$timestamp,df$power))+theme_bw() +geom_point()
       print(p)
       })
    })

When I run this application, following error is generated

Error in df$timestamp : object of type 'closure' is not subsettable

Whenever I run the same server script as a normal R script everything works properly, but while using same code snippet in shiny results in above error.

Note: I referred the same question at links 1 , 2, 3 , but without any success

Community
  • 1
  • 1
Haroon Lone
  • 2,837
  • 5
  • 29
  • 65
  • Then try changing" `p <- ggplot(df,aes(df$timestamp,df$power))+theme_bw() +geom_point()` to `p <- ggplot(df,aes(timestamp,power))+theme_bw() +geom_point()` – jeremycg Oct 15 '15 at 18:16
  • Thanks, it worked. I don't know the reason why the same code works in a standalone R script, but fails in shiny. Really, crazy. Please put your comment as an answer. – Haroon Lone Oct 15 '15 at 18:21
  • It's related to the way `ggplot` [scopes itself inside a function](https://stackoverflow.com/questions/22287498/scoping-of-variables-in-aes-inside-a-function-in-ggplot) – jeremycg Oct 15 '15 at 18:32

1 Answers1

0

You want to remove the df$ from inside the aes call:

p <- ggplot(df,aes(timestamp,power))+theme_bw() +geom_point()

This is because of how aes is scoped

jeremycg
  • 24,657
  • 5
  • 63
  • 74