0
votes

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

1
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
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 Rashid
It's related to the way ggplot scopes itself inside a function - jeremycg

1 Answers

0
votes

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