Add a new dataset to a dataset list, get Warning: Error in !: invalid argument type


#1
# Hello,
# i am trying to do :
#   1.    read a .csv file, add the new data to a exist list with a new name:   dataList <- list("iris"=iris,"mtcars"=mtcars),   dataList[[input$newdata]]<- read.csv(input$file1$datapath,....)
#   2.   select a dataset from the list  to view: selectInput("contentsIn","Choose a dataset:", choices=names(dataList), selected ="")
#
#   but get Warning: Error in !: invalid argument type.
#
#   I appreciate if you can provide any comments.
#
library(shiny)
library(DT)


dataList <- list("iris"=iris,"mtcars"=mtcars)
ui <- fluidPage(

  titlePanel("loading file"),

  sidebarLayout(

    sidebarPanel(

      fileInput("file1", "choose csv file",
                multiple = TRUE,
                accept = ("text/csv")),
      textInput("newdata", "Rename"),
      actionButton("change", "change"),
      hr(),
      radioButtons("sep", "separator",
                   choices = c(Comma=",", Semicolon = ";", Tab="\t"), selected=","),
      hr(),
      radioButtons("quote", "Quote", choices = c(None="", "Double Quote"='"', "Single Quote"="'"), selected = '"'),

      hr(),
      radioButtons("disp", "Display", choices = c(head="head", All="all"), selected = "head"),

      selectInput("contentsIn","Choose a dataset:", choices=names(dataList), selected ="")


    ),
    mainPanel(
      dataTableOutput("contentsOut")
    )
  )
)


server <- function(input, output){

  observeEvent(input$change, {
    
    dataList[[input$newdata]]<- read.csv(input$file1$datapath,
                  header = input$header,
                  sep = input$sep,
                  quote = input$quote)
  }, ignoreInit = TRUE)


  getdata <- reactive({

    get(input$contentsIn)
  })


  output$contentsOut <- renderDataTable({

    if (input$disp=="head") { return (datatable(head(getdata())))}
    else {
      return (datatable(getdata()))
    }
  })

}

shinyApp(ui, server)

#2

Hi @wechat614 there are a few issues to be addressed in your code.
Take a look at the following points:

  • you need to set header, this is NULL
  • use <<- to update dataList
  • in getdata use dataList[[input$contentsIn]] instead of get
  • update the select contentsIn after have the file loaded.

These steps might solve.


#3

Hi Wilson,
it is solved with your suggestion.
Thank you,
zhijun


#5
#updated code

library(shiny)

dataList <- list("iris"=iris,"mtcars"=mtcars)

ui <- fluidPage(

  titlePanel("loading file"),

  sidebarLayout(

    sidebarPanel(

      fileInput("file1", "choose csv file",
                multiple = TRUE,
                accept = ("text/csv")),
      textInput("newdata", "Rename"),
      actionButton("change", "change"),
      selectInput("contentsIn","Choose a dataset:", choices=names(dataList), selected =""),

      hr(),
      radioButtons("sep", "separator",
                   choices = c(Comma=",", Semicolon = ";", Tab="\t"), selected=","),
      hr(),
      radioButtons("quote", "Quote", choices = c(None="", "Double Quote"='"', "Single Quote"="'"), selected = '"'),

      hr(),
      radioButtons("disp", "Display", choices = c(head="head", All="all"), selected = "head")
    ),
    mainPanel(
      dataTableOutput("contentsOut")
    )
  )
)


server <- function(input, output, session){

  observeEvent(input$change, {

    dataList[[input$newdata]]<<- read.csv(input$file1$datapath,
                  header = TRUE,
                  sep = input$sep,
                  quote = input$quote)
    updateSelectInput(session,"contentsIn",
                      label = "Choose a dataset:",
                      choices = names(dataList),
                      selected = "")
  })



  getdata <- reactive({
    dataList[[input$contentsIn]]

  })


  output$contentsOut <- renderDataTable({

    if (input$disp=="head") { return (head(getdata()))}
    else {
      return (getdata())
    }
  })

}

shinyApp(ui, server)