Help to publish an app on shinyapps.io

Hello
I have an error message when i publish my app in shinyapps.io
The histogram is well displayed on my local machine but not on shinyapps
What is wrong please?

Thanks

On the web page shinyapps.io, you can connect to your dashboard, select the application, and you'll get a page with these tabs:

There you can click on "Logs" and see what the error is.

Hi
Thanks
Pearhaps thi is the root cause?
2023-04-07T05:25:28.641476+00:00 shinyapps[8782911]: Warning: Error in $: object of type 'closure' is not subsettable

Here is the server logic

# Define server logic required to draw a histogram
server <- function(input, output) {

  output$distPlot <- renderPlot({
 
    data <- read.csv("C:\\Users\\jro\\Documents\\INFORMATIQUE\\DATA SCIENCE\\dataset\\mtcars.csv", header=TRUE)
    x    <- data$mpg
    # generate bins
    bins <- seq(min(x), max(x), length.out = input$bins + 1)
        
    # draw the histogram with the specified number of bins
    hist(x, breaks = bins, col = 'green', border = 'white', xlab = 'mpg value', main = 'Histogram of JP', axes = TRUE, plot = TRUE)
    })
}

It does look like a cause.

This is a local path on your computer. When uploading to shinyapps.io, this path is probably not valid anymore. Keep the csv file in the same directory as the app script, upload it along with the rest of the app, and use a relative path. That should work better.

Here is what I done (I put the mycars.csv in the WebApp_JP2 folder and added a relative path) but I have the same problem....

# Define server logic required to draw a histogram
server <- function(input, output) {

  output$distPlot <- renderPlot({
    data = read.csv("~\\INFORMATIQUE\\DATA SCIENCE\\Shiny_App\\WebApp\\WebApp_JP2\\mtcars.csv", header=TRUE)
    x    <- data$mpg
    # generate bins
    bins <- seq(min(x), max(x), length.out = input$bins + 1)
        
    # draw the histogram with the specified number of bins
    hist(x, breaks = bins, col = 'green', border = 'white', xlab = 'mpg value', main = 'Histogram of JP', axes = TRUE, plot = TRUE)
      
    })
}

Still doesn't look like a relative path. Also, \\ is a file separator on Windows, if I'm not mistaken shinyapps.io runs on Linux, you should use the file separator /.

This works for me:

step 0, open RStudio in the project directory.

step 1, run:

write.csv(mtcars, "mtcars.csv")

this will create a csv file in the current directory

step 3, create the file app.R in the same directory, with this content:

library(shiny)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel = sidebarPanel(
      sliderInput("bins", "Type text", min = 0, max = 100, value = 50),
    ),
    mainPanel = mainPanel(
      plotOutput("distPlot")
    )
  )
)

server <- function(input, output, session) {
  output$distPlot <- renderPlot({
    
    data <- read.csv("mtcars.csv", header=TRUE)
    x    <- data$mpg
    # generate bins
    bins <- seq(min(x), max(x), length.out = input$bins + 1)
    
    # draw the histogram with the specified number of bins
    hist(x, breaks = bins, col = 'green', border = 'white', xlab = 'mpg value', main = 'Histogram of JP', axes = TRUE, plot = TRUE)
  })
}
shinyApp(ui, server)

Step 4, upload it to shinyapps.io (I use the blue button on top of the script area). Make sure you upload only these two files: app.R and mtcars.csv.

Step 5: it works for me.

To be more explicit, ~ refers to your home directory, on Windows, your Documents directory. So this is an absolute path, that translates to:

C:/Users/me/Documents/INFORMATIQUE/...

But on the shinyapps server, maybe the correct path would be something like

/srv/users_apps/JP_apps/WebApp_JP2/mtcars.csv

(we don't really know, and don't need to know, how the server organizes its files).

So the solution is to only store files inside the WebApp_JP2 directory (or subdirectories of it), and have paths that start in there, like

mtcars.csv

or

my_subdirectory/mtcars.csv

Importantly, these paths can not start with C:\, / or ~, or they're absolute paths.

For more on absolute and relative paths there are some examples here for Windows and here for Linux. One important aspect is R was initially written with Linux in mind, and uses / as file separator, and \ is used for special characters. When running R on Windows, you can still use / as file separator, it will get automatically translated to \ when getting files, or you can use \\ which is equivalent.

Many thanks Alexis, now it works fine :wink:

1 Like

This topic was automatically closed 7 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.