Itai lotan
Itai lotan

Reputation: 11

Creating an app using shiny to plot a csv file

I am trying to create a shiny app in R that uses a CSV file I have but I keep getting an error when trying to publish it. Here is my code:

library(shiny)
library(plotly)
library(dplyr)
library(ggplot2)
library(DT)

# Define UI for application that draws a histogram
ui <- fluidPage(# Application title
    titlePanel("Hello"),
    
    
    
    # Show a plot of the generated distribution
    mainPanel(fig))


server <- function(input, output) {
    output$table <- renderDT(investors)
    output$fig <- renderPlot({
        #
        count_distinct_investors = investors %>% group_by(Marital_Status) %>% summarize(count =
                                                                                            n())
        
        p = ggplot(count_distinct_investors,
                   aes(
                       x = reorder(Marital_Status,-count),
                       y = count,
                       fill = Marital_Status
                   )) + geom_bar(stat = "identity") +
            coord_flip() + labs(title = "investors Count") + labs(x = "Marital_Status")
        
        fig = ggplotly(p)
        fig
        
        
        
    })
}

# Run the application
shinyApp(ui = ui, server = server)

`

I have pre-loaded to file to the environment which is called investors and has a column called Marital_Status. This is the error I am getting when trying to publish the app:`The application failed to start:

exited unexpectedly with code 1

Loading required package: ggplot2

Attaching package: ‘plotly’

The following object is masked from ‘package:ggplot2’:

    last_plot

The following object is masked from ‘package:stats’:

    filter

The following object is masked from ‘package:graphics’:

    layout


Attaching package: ‘dplyr’

The following objects are masked from ‘package:stats’:

    filter, lag

The following objects are masked from ‘package:base’:

    intersect, setdiff, setequal, union


Attaching package: ‘DT’

The following objects are masked from ‘package:shiny’:

    dataTableOutput, renderDataTable

Error in value[[3L]](cond) : object 'fig' not found
Calls: local ... tryCatch -> tryCatchList -> tryCatchOne -> <Anonymous>
Execution halted

Upvotes: 1

Views: 242

Answers (1)

KacZdr
KacZdr

Reputation: 1529

If you want plotly to be your output, you need to remember the following:

  1. In server: output$fig <- renderPlotly instead of output$fig <- renderPlot
  2. In UI: mainPanel(plotlyOutput("fig")) instead of mainPanel(fig))
  3. You don't have to assign plotly to an object, just leave ggplotly(p) at the end

You can try this code to see how it works:

library(shiny)
library(ggplot2)
library(ggthemes)
library(plotly)

ui <- fluidPage(  
  titlePanel("Plotly"),
  sidebarLayout(
    sidebarPanel(),
    mainPanel(
      plotlyOutput("plot2"))
    ))

server <- function(input, output) {
  
  output$plot2 <- renderPlotly({
    ggplotly(
      ggplot(data = mtcars, aes(x = disp, y = cyl)) + 
        geom_smooth(method = lm, formula = y~x) + 
        geom_point() + 
        theme_gdocs())
  })
}

shinyApp(ui, server)

Upvotes: 1

Related Questions