Not able to call R script in

Below is the code in R shiny where output is a customer Report generated from a datatable. A custom button named "test" is added in datatable output on click of which I am planning to output a visualization using R script. But below code is not able to call R script. The ListTcCodesCityWise.R has logic to connect to ODBC and generate a barplot. I tried system, Model dialog and paste function in observeEvent function to launch R script but it didn't work. Is there any way to call R script that will generate a new output in UI on top of datatable output.

library(shiny)
library(DT)

ui <- basicPage(
  DTOutput("dtable")
)

server <- function(input, output, session){
  output$dtable <- renderDT(
    datatable(CustomerReport,
              extensions = 'Buttons',
              options = list(
                dom = 'Bfrtip',
                buttons = list(
                  "copy",
                  list(
                    extend = "collection",
                    text = 'test',
                    action = DT::JS("function ( e, dt, node, config ) {
                                      Shiny.setInputValue('test', true);
                                   }")
                  )
                )
              )
    )
  )

  observeEvent(input$test, {
    if(input$test){
      Source("ListTcCodesCityWise.R")
    }
  })
}

shinyApp(ui, server)

Hello @gurpreet.raina, I would create a function to generate the new UI component needed. Then, instead of sourcing a script each button press you can call the new function. I modified the code you provided to include this different approach,

library(shiny)
library(DT)

# Now this file creates a function to generate the UI component. I'll call 
# this function "list_tc_codes_city_wise", see usage below. 
# We only need to source the file once now in the lifetime of the shiny 
# application.
source("ListTcCodesCityWise.R") 

ui <- ..

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

  observeEvent(input$test, {
    if (input$test) {
      list_tc_codes_city_wise(..)  # <= call new function instead of sourcing
    }
  }
}

shinyApp(ui, server)

If I wasn't clear about any details I am happy to elaborate. I hope this helps.

Hi Nathan teetor,

First of all thank you for the reply!. Well, it would be great if you can elaborate more on the fix. Here is the revised code as instructed in your reply. But when I run this code in RSTUDIO then it can't find function list_tc_codes_city_wise (Warning: Error in list_tc_codes_city_wise: could not find function "list_tc_codes_city_wise"
[No stack trace available]). I have wrapped ListTcCodesCityWise.R file code in a function named list_tc_codes_city_wise. I am missing anything?

setwd("C:/Users/gurprees/Documents/Rsmartpack/")
source("ListTcCodesCityWise.R")

ui <- basicPage(
DTOutput("dtable")
)

server <- function(input, output, session){
output$dtable <- renderDT(
datatable(CustomerReport,
extensions = 'Buttons',
options = list(
dom = 'Bfrtip',
buttons = list(
"copy",
list(
extend = "collection",
text = 'test',
action = DT::JS("function ( e, dt, node, config ) {
Shiny.setInputValue('test', true);
}")
)
)
)
)
)

observeEvent(input$test, {
if(input$test){
list_tc_codes_city_wise()
}
})
}

shinyApp(ui, server)

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.