I made a package that contains a lot of functions I use at work. I'm currently working on a shiny interface to one of them and would like to include the shiny function in the same package. A really abbreviated structure of the relevant part of my package is:
My_Package
-- /R/
------ func.R
------ funcShinyApp.R
Sample code below, but briefly: the user uploads an Excel file, sets a few other options, and then runs the function by clicking on an actionButton. The problem is, nothing happens when the user clicks on the actionButton.
The function itself runs correctly when used outside of shiny. I also know that the file upload part works because I show the uploaded file after it's read in. There's also a call to an unexported function in my package that works fine, which nixes the theory I had before that shiny just didn't know where the main function lived. I know this is not a reproducible example (the actual code is too long and I can't share the data the function uses), but I wanted to check whether the way I set this up is something that even should work.
ui <- fluidPage(
tabsetPanel(id='tabset',
tabPanel(title='Upload',
fileInput(inputId='file',
label='File',
accept=c('.csv','.xls','.xlsx','.Rdata'),
buttonLabel='Upload...'),
tableOutput(outputId='file_preview'),
tabPanel(title='Options',
#... a lot of different *Input() functions here
)
tabPanel(title='Run',
actionButton(inputId='runFunc',
label='Run!'))
)
)
server <- function(input,output,session) {
options(shiny.maxRequestSize=5*1024^3)
session$onSessionEnded(stopApp)
input <- reactive({
if (!is.null(input$file)) {
fileext <- My_Package:::get_filenamestr_ext(input$file$datapath) # this is the unexported function that DOES work
if (fileext=='csv') {
read.csv(file=input$file$datapath,header=TRUE)
} else if (fileext %in% c('xls','xlsx')) {
readxl::read_excel(path=input$file$datapath,sheet=1)
} else if (fileext=='Rdata') {
load(input$file$datapath)
}
}
})
observe({
output$file_preview <- renderTable(input(),striped=TRUE,hover=TRUE,bordered=TRUE,width='80%')
})
res <- eventReactive(input$runFunc,{
MyFunc(items=input(), # this is the function call that does not work
#... other options here,
)
})
}
shinyApp(ui=ui,server=server)