Reputation: 11
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
Reputation: 1529
If you want plotly to be your output, you need to remember the following:
output$fig <- renderPlotly
instead of output$fig <- renderPlot
mainPanel(plotlyOutput("fig"))
instead of mainPanel(fig))
ggplotly(p)
at the endYou 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