Problem with a package (readr) that does not install when deploying my shiny application

Hello,

I'm trying to publish a shiny application on connect.
My application has the following script:

library(shiny)
library(googleCloudStorageR)
library(gargle)

if(!require('readr')) {
install.packages('readr')
library('readr')
}

if(!require('dplyr')) {
install.packages('dplyr')
library('dplyr')
}

if(!require('dygraphs')) {
install.packages('dygraphs')
library('dygraphs')
}

if(!require('plotly')) {
install.packages('plotly')
library('plotly')
}

if(!require('PerformanceAnalytics')) {
install.packages('PerformanceAnalytics')
library('PerformanceAnalytics')
}

if(!require('shinydashboard')) {
install.packages('shinydashboard')
library('shinydashboard')
}

if(!require('xts')) {
install.packages('xts')
library('xts')
}

if(!require('zoo')) {
install.packages('zoo')
library('zoo')
}

if(!require('rjson')) {
install.packages('rjson')
library('rjson')
}

if(!require('googleAuthR')) {
install.packages('googleAuthR')
library('googleAuthR')
}

scope <-c("https://www.googleapis.com/auth/cloud-platform")
token <- token_fetch(scopes = scope)
gcs_auth(token = token))

p1 <- gcs_get_object("p1.csv", bucket = "my_bucket")

p2 <- gcs_get_object("p2.csv", bucket = "my_bucket")

p3 <- gcs_get_object("p3.csv", bucket = "my_bucket")

portfolio_choices <- c(
"Conservative" = "conservative_portfolio_returns",
"Balanced" = "balanced_portfolio_returns",
"Aggressive" = "aggressive_portfolio_returns"
)

ui <- dashboardPage(
dashboardHeader(title = "Portfolio Dashboard"),
dashboardSidebar(
selectInput(
"portfolio",
"Choose a portfolio",
choices = portfolio_choices,
selected = "balanced_portfolio_returns"
),
dateInput(
inputId = "date",
label = "Starting Date",
value = "2010-01-01",
format = "yyyy-mm-dd"
),
sliderInput("mar", "Min Acceptable Rate", min = 0, max = 0.1, value = 0.008, step = 0.001),
numericInput("window", "Rolling Window", min = 6, max = 36, value = 12)
),
dashboardBody(
fluidRow(
box(title = "Rolling Sortino", width = 12,
plotlyOutput("time_series")
)
),
fluidRow(
box(title = "Scatterplot", width = 4,
plotlyOutput("scatterplot", height = 250)
),
box(title = "Histogram", width = 4,
plotlyOutput("histogram", height = 250)
),
box(title = "Density", width = 4,
plotlyOutput("density", height = 250)
)
)
)
)

server <- function(input, output) {

rate_limit_sec <- 2

portfolio_selected <- throttle(reactive({
req(input$portfolio, input$date)
if (input$portfolio =="conservative_portfolio_returns") {
returns <- p1}
else if(input$portfolio=="balanced_portfolio_returns"){
returns <- p2}
else {
returns <- p3}

returns %>% 
  as_tibble() %>%
  #collect() %>%
  mutate(date = as.Date(date)) %>%
  filter(date >= input$date)

}), rate_limit_sec * 1000)

rolling_sortino <- reactive({
req(input$mar)
req(input$window)

portfolio_selected()$returns %>%
  xts::xts(order.by = portfolio_selected()$date) %>%
  rollapply(input$window, function(x) SortinoRatio(x, MAR = input$mar)) %>%
  `colnames<-`("24-rolling")

})

sortino_byhand <- reactive({
portfolio_selected() %>%
mutate(ratio = mean(returns - input$mar) / sqrt(sum(pmin(returns - input$mar, 0)^2) / nrow(.))) %>%
# Add two new columns to help with ggplot.
mutate(status = ifelse(returns < input$mar, "down", "up"))
})

output$time_series <- renderPlotly({
plot_ly() %>%
add_lines(x = index(rolling_sortino()), y = as.numeric(rolling_sortino())) %>%
layout(
hovermode = "x",
xaxis = list(
rangeslider = list(visible = TRUE),
rangeselector = list(
x = 0, y = 1, xanchor = 'left', yanchor = "top", font = list(size = 9),
buttons = list(
list(count = 1, label = 'RESET', step = 'all'),
list(count = 1, label = '1 YR', step = 'year', stepmode = 'backward'),
list(count = 3, label = '3 MO', step = 'month', stepmode = 'backward'),
list(count = 1, label = '1 MO', step = 'month', stepmode = 'backward')
)
)
)
)
})

output$scatterplot <- renderPlotly({
portfolio_scatter <- ggplot(sortino_byhand(), aes(x = date, y = returns, color = status) )+
geom_point() +
geom_vline(xintercept = as.numeric(as.Date("2016-11-30")), color = "blue") +
geom_hline(yintercept = input$mar, color = "purple", linetype = "dotted") +
scale_color_manual(values = c("tomato", "chartreuse3")) +
theme(legend.position = "none") + ylab("percent monthly returns")

ggplotly(portfolio_scatter) %>% 
  add_annotations(
    text = "Trump", x = as.numeric(as.Date("2016-11-30")), 
    y = -.05, xshift = -10, textangle = -90, showarrow = FALSE
  )

})

output$histogram <- renderPlotly({
p <- ggplot(sortino_byhand(), aes(x = returns)) +
geom_histogram(alpha = 0.25, binwidth = .01, fill = "cornflowerblue") +
geom_vline(xintercept = input$mar, color = "green")
ggplotly(p) %>%
add_annotations(text = "MAR", x = input$mar, y = 10, xshift = 10, showarrow = FALSE, textangle = -90)
})

output$density <- renderPlotly({
sortino_density_plot <- ggplot(sortino_byhand(), aes(x = returns)) +
stat_density(geom = "line", size = 1, color = "cornflowerblue")

shaded_area_data <- ggplot_build(sortino_density_plot)$data[[1]] %>%
  filter(x < input$mar)

sortino_density_plot <-
  sortino_density_plot +
  geom_area(data = shaded_area_data, aes(x = x, y = y), fill = "pink", alpha = 0.5) +
  geom_segment(
    data = shaded_area_data, aes(x = input$mar, y = 0, xend = input$mar, yend = y),
    color = "red", linetype = "dotted"
  )

ggplotly(sortino_density_plot) %>%
  add_annotations(
    x = input$mar, y = 5, text = paste("MAR =", input$mar, sep = ""), textangle = -90
  ) %>%
  add_annotations(
    x = (input$mar - .02), y = .1, text = "Downside", 
    xshift = -20, yshift = 10, showarrow = FALSE
  )

})
}

shinyApp(ui = ui, server = server)

At the time of deployment I get the following error :

I don't understand because I have installed and loaded the "readr" package in my script.

Can you help me please ?

You should not include install commands in a shiny app, just load the library and rsconnect will replicate your local environment when the app is deployed.

Hello, thank you for your answer.

I have the same problem when I write this in the script

library('shiny')
library('googleCloudStorageR')
library('gargle')
library('readr')
library('dplyr')
library('dygraphs')
library('plotly')
library('PerformanceAnalytics')
library('shinydashboard')
library('xts')
library('zoo')
library('rjson')
library('googleAuthR')

Try reinstalling readr (locally) before deploying, do you get any error message?

install.packages("readr")

If so, please post the complete console output you get when you run this command.

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.