简体   繁体   中英

How to fix the error "file must be a character string or connection" when running my Shiny app in R?

My application is giving me problems once it is asked to execute the function Conjoint. If I use the function only on R there are no problems, the problems appear when I use it in my shiny app.

library(shiny)
library(conjoint)

ui <- fluidPage(
  fileInput('file1', 'Carica il file delle preferenze',
            accept = c("text/csv",
                       "text/comma-separated-values,text/plain",
                       ".csv")),
  fileInput('file2', 'Carica il file del profilo',
            accept = c("text/csv",
                       "text/comma-separated-values,text/plain",
                       ".csv")),
  fileInput('file3', 'Carica il file dei nomi dei livelli',
            accept = c("text/csv",
                       "text/comma-separated-values,text/plain",
                       ".csv")),
  textOutput("TEST")
  
)


server <- function(input, output) {
  
  pref <- reactive({
    req(input$file1)
    read.csv(input$file1$datapatht)
  })
  
  profiles <- reactive({
    req(input$file2)
    read.csv(input$file2$datapatht)
  })
  
  levelnames <- reactive({
    req(input$file3)
    read.csv(input$file3$datapatht)
  })
  

  
  output$TEST<-renderTable({
    Conjoint(y=pref(),x=profiles(),z=levelnames())
    })
}

shinyApp(ui, server)

You are receiving the error in the question title because of a typo: datapatht should be datapath . I also recommend simply using accept = ".csv" since any .csv file must necessarily have that file ending.

Both are implemented below:

library(shiny)
library(conjoint)

ui <- fluidPage(
  fileInput('file1', 'Carica il file delle preferenze',
            accept = c(".csv")),
  fileInput('file2', 'Carica il file del profilo',
            accept = c( ".csv")),
  fileInput('file3', 'Carica il file dei nomi dei livelli',
            accept = c(".csv")),
  textOutput("TEST")
  
)


server <- function(input, output) {
  
  pref <- reactive({
    req(input$file1)
    read.csv(input$file1$datapath)
  })
  
  profiles <- reactive({
    req(input$file2)
    read.csv(input$file2$datapath)
  })
  
  levelnames <- reactive({
    req(input$file3)
    read.csv(input$file3$datapath)
  })
  

  
  output$TEST<-renderTable({
    Conjoint(y=pref(),x=profiles(),z=levelnames())
    })
}

shinyApp(ui, server)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM