Create a Shiny App to Upload a Stata Dataset and convert it into a SPSS File

I'm trying to create a shiny app to let people upload a file, and the web page executes rio::convert() function, to transform a ".dta" file into a ".sav" file.

Is this possible? How can you code the UI and server?

I made an attempt:

ui.R

## -----
## ui.R
## -----

library(shiny)
library(markdown)

shinyUI(fluidPage(sidebarLayout(

  sidebarPanel(fileInput('infile', label = "Datos en Stata (Máx. 30 MB)", buttonLabel = "Subir..."),
               downloadButton('downloadData', 'Bajar Base')
  ),
  mainPanel(
    h3("Muestra de Datos"),
    tableOutput("contents")
    )
)))

server.R

options(shiny.maxRequestSize = 30*1024^2)
## --------
## server.R
## --------

library(rio)
library(shiny)
library(tools)
library(foreign)

server <- function(input, output) {
    getData <- reactive({
    inFile <- input$infile
    if (is.null(input$infile))
      return(NULL)
      rio::import(inFile$datapath)
  })
    getData_proc <- reactive({
      rio::export(file= "data.sav", x=getData())
      })
  output$contents <- renderTable(
    head(getData())
  )
  output$downloadData <- downloadHandler(
    filename = function() {
      paste("data", Sys.Date(), ".sav", sep="")
    },
    content = function(x) {
   #   rio::convert(input$infile$datapath, "mtcars.sav")
      #https://www.rdocumentation.org/packages/rio/versions/0.5.16/topics/export
      #https://stackoverflow.com/questions/57493392/how-to-fix-file-not-found-when-using-the-downloadhandler
      rio::export(getData(),file=x)
    }
)  
}

The webpage preliminary looks like this. My main problem is that when I upload a file and try to download it, the file that I can download a text file that when I download it shows the following error: "Failed - Server problem".

You can see this problem here: Downloads a text document, that ends with an error

The log of the shinyapps webpage get the following message:

Server version: 1.8.0.3-19 R version: 3.6.2 shiny version: 1.4.0

rmarkdown version: (none) LANG: es_MX.UTF-8 knitr version: (none)

Using pandoc: /opt/connect/ext/pandoc2 jsonlite version: 1.6 httpuvversion: 1.5.2 htmltools version: 0.4.0 RJSONIO version: (none)

Using jsonlite for JSON processing: Starting R with process ID: '152'

Listening on http://127.0.0.1:35337 Warning: Error in write_sav_:

Writing failure: A provided name begins with an illegal character.

[No stack trace available]:

Thanks in advance

Upvotes: 0

Views: 485

Answers (1)

In fact, I found that the problem in my app was in my dataset. This data contained many unformatted names, I resolved by using the function clean_names from janitor package. The corrected code is presented below:

## --------
## server.R
## --------

library(rio)
library(shiny)
library(tools)
library(foreign)
library(janitor)
library(lubridate)


server <- function(input, output) {
    getData <- reactive({
    inFile <- input$infile
    if (is.null(input$infile))
      return(NULL)
    haven::read_dta(inFile$datapath, encoding = "latin1")
  })
    getData_proc <- reactive({
      if (is.null(input$infile))
        return(NULL)
      janitor::clean_names(getData())
      #names(eso) <- gsub("-|\\.|\\/|'|\\[|\\]","",names(eso))
      #names(eso) <- gsub(" ","_",names(eso))
      })
  output$contents <- renderTable(
    head(getData_proc())
  )
  output$downloadData <- downloadHandler(
    filename = paste0("data_",format(Sys.time(), '%Y_%m_%d'),"_",hour(Sys.time()),"_",minute(Sys.time()),".sav"),
    content = function(x) {
   #   getData_proc()
      #rio::convert(getData(), "mtcars.sav")
      #https://www.rdocumentation.org/packages/rio/versions/0.5.16/topics/export
      #https://stackoverflow.com/questions/57493392/how-to-fix-file-not-found-when-using-the-downloadhandler
      rio::export(getData_proc(),file=x)
    }
)

}

Upvotes: 1

Related Questions