A.M. Das
A.M. Das

Reputation: 81

How to execute a long R script as function in RShiny and display the solution in the ui?

I am trying to run an R Script as a R Shiny App. From the ui, the user will upload a .csv file and input 4 numeric variables. These variables should be passed through the function and it will generate a final_table which should be displayed as output in the Shiny App. Currently, the variables are being passed through the function but not resulting in the final table. I am new with RShiny, appreciate your help in making this work.

my_function.R is the script file which contains the function my_function(). This in fact is a 500 line R script compressed into a function for ease of use.

my_function <- function(tbl_load, ts_freq, ts_start_yr, ts_start_month, seasonal_cat) {
  ..........  
  return(collect_ALL_final_fair)
}

ui.R

library(shiny)
fluidPage(
titlePanel("Elasticity Tool"),
sidebarLayout(
sidebarPanel(
  fileInput('file1', 'Choose datatable csv file', accept=c('.csv')),

  numericInput("ts_freq", "Time series frequency:", 52, min = 1, max = 100),
  numericInput("ts_start_yr", "Starting year:", 2013, min = 1990, max = 2030),
  numericInput("ts_start_month", "Starting month:", 3, min = 1, max = 12),
  numericInput("seasonal_cat", "Seasonal Category", 0, min = 0, max = 1),
  br(),

  actionButton("goButton", label = "Run tool"),
  br()      
),

mainPanel(
  tabsetPanel(type = 'tabs', 
              tabPanel("Output", tableOutput('contents2'))
    )
  )
 )
)

server.R

library(shiny)
library(datasets)
source("my_function.R")
#packages
library("glmnet")
library(Matrix)
library(dplyr)
library(forecast)
library(zoo)
library(stats)
library(car)
options(scipen = 999)

shinyServer(function(input, output) {

observeEvent(input$goButton, {
  tbl_load <- input$file1
  ts_freq <- input$ts_freq
  ts_start_yr <- input$ts_start_yr
  ts_start_month <- input$ts_start_month
  seasonal_cat <- input$seasonal_cat
  output$contents2 <- renderDataTable({
    my_function(tbl_load, ts_freq, ts_start_yr, ts_start_month, seasonal_cat)
    })
   })
  })

Upvotes: 1

Views: 1913

Answers (1)

B&#225;rbara Borges
B&#225;rbara Borges

Reputation: 919

It seems like your output-render pairs are mismatched. If you want to use a regular table, you should have:

# ui
tableOutput('contents2')

# server
output$contents2 <- renderTable({})

If you want to use a datatable, you should have:

# ui
DT::dataTableOutput('contents2')

# server
output$contents2 <- DT::renderDataTable({})

If you want to do this, make sure you've installed the DT package.


If this turns out to not be the only problem, include the function definition in the server.R file (after you load the appropriate libraries). If this fixes it, it's because you weren't sourcing the file correctly (maybe your path isn't right or something like that). If it still doesn't work, the problem is in your function itself.

Also, why aren't you passing the inputs directly to my_function? You should also avoid using an observer in this case. Instead you can use this pattern:

my_table <- eventReactive(input$goButton, {
  my_function(input$file1, input$ts_freq, input$ts_start_yr, 
              input$ts_start_month, input$seasonal_cat)
})

output$contents2 <- renderTable({
  my_table()
})

Upvotes: 1

Related Questions