Function not found when using nested modules

510 views
Skip to first unread message

Sebastien Bihorel

unread,
May 12, 2016, 9:09:02 PM5/12/16
to Shiny - Web Framework for R
Hi,

I am trying to create a working example of nested modules based upon the instructions and examples provided in http://shiny.rstudio.com/articles/modules.html, but hit a wall... My app include 2 files:

app.R
library(shiny)
library
(shinydashboard)

source
('modules.R')

ui
<- fluidPage(
 
  sidebarLayout
(
    sidebarPanel
(
      mainUI
("test")
   
),
    mainPanel
(
      dataTableOutput
("table"),
      dataTableOutput
("table2")
   
)
 
)
)

server
<- function(input, output, session) {
  datafiles
<- callModule(main, "test")
 
  output$table
<- renderDataTable({
      datafiles
()$df1()
   
})
  output$table2
<- renderDataTable({
      datafiles
()$df2()
   
})
}

shinyApp
(ui, server)


modules.R

csvFileInput
<- function(id, label = "CSV file") {
 
# Create a namespace function using the provided id
  ns
<- NS(id)
 
  tagList
(
    fileInput
(ns("file"), label),
    checkboxInput
(ns("heading"), "Has heading"),
    selectInput
(ns("quote"), "Quote", c(
       
"None" = "",
       
"Double quote" = "\"",
       
"Single quote" = "'"
     
))
 
)
}

mainUI
<- function(id){
 
  ns
<- NS(id)
 
  box
(cvsFileInput(ns('datafile1'), "User data (.csv format)"),
    title
= paste('box',1),
    collapsible
= FALSE,
    width
= 6,
    status
= 'info',
    solidHeader
= TRUE)
  box
(cvsFileInput(ns('datafile2'), "User data (.csv format)"),
    title
= paste('box',2),
    collapsible
= FALSE,
    width
= 6,
    status
= 'info',
    solidHeader
= TRUE)
 
}

csvFile
<- function(input, output, session, stringsAsFactors) {
 
# The selected file, if any
  userFile
<- reactive({
     
# If no file is selected, don't do anything
      validate
(need(input$file, message = FALSE))
      input$file
   
})
 
 
# The user's data, parsed into a data frame
  dataframe
<- reactive({
      read
.csv(userFile()$datapath,
        header
= input$heading,
        quote
= input$quote,
        stringsAsFactors
= stringsAsFactors)
   
})
 
 
# Return the reactive that yields the data frame
 
return(dataframe)
}

main
<- function(input, output, session){
 
  datafile1
<- callModule(csvFile, "datafile1",
    stringsAsFactors
= FALSE)
  datafile2
<- callModule(csvFile, "datafile2",
    stringsAsFactors
= FALSE)
 
 
return(list(df1=datafile1(), df2=datafile2()))
 
}

When I execute the app, I get an error message saying: ERROR: could not find function "cvsFileInput"

I though my code was straight-up application of the instructions but there is obviously something wrong.

Thanks you in advance for your help

Sebastien

Sebastien Bihorel

unread,
May 13, 2016, 12:47:11 PM5/13/16
to Shiny - Web Framework for R
Never mind. There was a typo in the name of the function calls. I was so focused on the namespace issue, that I did not think about check the actual name of the function.

Plus, there are other problems with the code, like using reactives in a return call.

I guess learning through pain is needed...
Reply all
Reply to author
Forward
0 new messages