Search code examples
rshinyubuntu-serverrhandsontable

Permission denied while using write.csv on ubuntu server


I have this R app that I use to display and modify at the same time a table, using rHandsontable. It is hosted on a Ubuntu virtual machine. Problem is, when I run the code on my computer, everything works just fine. However, when it is ran on my virtual machine (Ubuntu 14.04.5 LTS (GNU/Linux 3.13.0-135-generic x86_64)), it crashes when I try to save it. I get the following error :

The application unexpectedly exited.

Diagnostic information has been dumped to the JavaScript error console.

When I look inside the logs, I see this :

Loading required package: xlsxjars

Listening on http://127.0.0.1:53183
Warning in file(file, ifelse(append, "a", "w")) :
  cannot open file 'listes/liste_traiteur.csv': Permission denied
Warning: Error in file: cannot open the connection
Stack trace (innermost first):
    62: file
    61: write.table
    60: eval
    59: eval
    58: eval.parent
    57: write.csv
    56: observerFunc [/srv/shiny-server/app.R#187]
     1: runApp

Here is the code :

# before UI and server 
filename <- as.character("liste_traiteur")
file <- paste0(filename, ".csv")
liste_menu <- read.csv(file = file, header = TRUE, sep = ",")

fname <- file # R object data frame stored as ASCII text
values <- list() 
setHot <- function(x) values[["hot"]] <<- x 

# inside the server
      observe({ 
      input$saveBtn # update csv file each time the button is pressed
      if (!is.null(values[["hot"]])) { # if there's a table input
        write.csv(values[["hot"]], fname) # overwrite the temporary database file
        write.csv(x = values[["hot"]], file = fname, row.names = FALSE) # overwrite the csv
        shinyjs::hide("modification")
        shinyjs::show("modif_reussie")
        shinyjs::reset("form")
        liste_menu <- read.csv(file = fname, header = TRUE, stringsAsFactors = FALSE)
      }
    })
      output$hot <- renderRHandsontable({ 

        if (!is.null(input$hot)) { # if there is an rhot user input...
          DF <- hot_to_r(input$hot) # convert rhandsontable data to R object and store in data frame
          setHot(DF) # set the rhandsontable values

        } else {
          DF <- read.csv(file = fname, header = TRUE, stringsAsFactors = FALSE) # else pull table from the csv (default)
          setHot(DF) # set the rhandsontable values
        }

      rhandsontable(DF) %>% # actual rhandsontable object
        hot_table(highlightCol = TRUE, highlightRow = TRUE, readOnly = FALSE) %>%
        hot_col("Item", readOnly = FALSE) %>%
        hot_col("Prix", readOnly = FALSE)
    })

fname is the file we're reading then overwriting when the user modifies it using the displayed rhandsontable object. It is displayed like this in the UI :

  tabPanel("Liste de prix", 
           fluidPage(
             shinyjs::useShinyjs(), 
             shinyjs::inlineCSS(appCSS),
             fluidRow(
               div(
                 id = "modification",
                 column(12,
           rHandsontableOutput("hot"),
           br(),
           actionButton("saveBtn", "Enregistrer", icon = icon("floppy-o")))
               )),
           fluidRow(
                    shinyjs::hidden(
                      div(
                        id = "modif_reussie",
                        column(12,
                        h3("La modification a été effectuée avec succès."),
                        actionLink("autre_modif", "Revenir à la page")
                      ))))
           ))

The error happens when the actionButton "saveBtn" is actioned. The code from the interactive table is originally from :

https://gist.github.com/cxbonilla/f49a2c7dbcfa23e6931b83838fad892d

Can someone help? This is really weird since another part of my app actually reads and write .csv perfectly - both on my computer and on the VM. However it does so with basic R functions and not rHandsontable. So I'm not quite understanding what's happening here.

Thanks!


Solution

  • Your shiny server doesn't have rights to save files to your desired folder. Something like this might help:

    sudo chown shiny:shiny /var/shiny-server/myfolder