4

I have a shiny app that opens with a simple .bat file that executes R and the script run.r in the background. The shiny use the package DT extensively to render all the tables. The problem i'm having is that if i run the shiny from Rstudio run app it shows all the tables but if I execute the shiny with the .bat file it just doesn't show theme. I have done this like 4 times and it is the first time it happens and I don't know the problem. I have the latest version of the pacakges of the DT available in CRAN,

So my server.r is :

server <- function(input, output,session) {
  observeEvent(input$run,{

TablasVaR <- function(mat,DT = T){
      mat_tbl <- data.frame(Activos = rownames(mat),Porcentaje = mat[,"Porcentaje"],
                            VaR = mat[,"Nivel"])

      tabla <- datatable(mat_tbl, escape = T,rownames = FALSE, 
                         selection = list(target = 'row'),
                         options = list(dom = 'tip', paging = TRUE))%>%
        formatStyle(1:ncol(mat_tbl),fontSize = '100%')%>%
        formatCurrency(3,digits = 0)%>%
        formatPercentage(2,digits = 1)
      if(DT){
        return(tabla)
      } else{
        return(mat_tbl)
      }

    }

    matr <- data.frame(Porcentaje=rnorm(19),Nivel = rnorm(19))



  output$table <- renderDataTable({TablasVaR(matr)})
  })
  session$onSessionEnded(function() {
    stopApp()
  })  
}

the ui.r is

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(),
    wellPanel(style = "background-color: #ffffff;",
              bsButton("run","run1",block=F, style="default"),
              fluidRow(column(4,align="center",offset = 4,
                              dataTableOutput("table"))))
  ))

the run.r is:

librerias <- c("openxlsx","ggplot2","scales","rugarch","zoo","data.table","stringr",
               "DT","plotly","lubridate","knitr","gridExtra","grid","shinyBS",
               "rmarkdown","nloptr","shiny")
if(length(setdiff(librerias, rownames(installed.packages()))) > 0){
  install.packages(setdiff(librerias, rownames(installed.packages())))
}
invisible(sapply(librerias, require, character.only = TRUE))
CAMINO <<- "D:/Users/aandr/Documents/Ejemplo/"
runApp(CAMINO, launch.browser=TRUE)

and the .bat file contains:

"C:\Program Files\R\R-3.5.1\bin\R.exe" CMD BATCH "run.r"

if I run the shiny app from the run.r the DT is display, but if I run it from the .bat file it wont. To make it run you will need to save the server.r, ui.r, run.r and .bat in the same folder.

Alejandro Andrade
  • 2,196
  • 21
  • 40

1 Answers1

6

If you read RStudio's page on Using DT in Shiny, you may not have noticed

Note that in DT, DTOutput() is an alias of dataTableOutput(), and renderDT() is an alias of renderDataTable(). You are recommended to use DTOutput() and renderDT() to avoid possible collisions with functions of the same names in shiny (shiny::dataTableOutput() and shiny::renderDataTable()).

Collisions, that's your problem. To confirm, if you see this:

find("dataTableOutput")
# [1] "package:DT"    "package:shiny"
find("renderDataTable")
# [1] "package:DT"    "package:shiny"

then the function name collision is likely to blame. Try replacing your dataTableOutput(...) with either DT::dataTableOutput(...) or DTOutput(...); and replace renderDataTable(...) with either DT::renderDataTable(...) or renderDT(...).

r2evans
  • 141,215
  • 6
  • 77
  • 149
  • I have not seen the `DTOutput` functions before but that's exactly the problem, so annoying it work on Rstudio for some reason and in the R console it didn't. Thanks – Alejandro Andrade Oct 24 '18 at 16:26
  • That's a pretty bad collision to have, especially when both packages are authored by the same group. (tidyverse suffers from this, too.) My preference has been to be specific with `DT::renderDataTable`, but the aliases work fine, too. Glad to help. (I've been considering using `loadNamespace(...)` instead of `library`, in part to force me to use `DT::`. I haven't found where this breaks, though I'm sure I'm missing something. BTW: using `require` in your code is incorrect: if you don't check its return then use `library`. Ref: https://stackoverflow.com/a/51263513/3358272 – r2evans Oct 24 '18 at 16:33
  • 1
    I used to use the DT:: in previous shiny apps i don't now why i change that in this one but won't do it never again. Thanks for the `require``comment – Alejandro Andrade Oct 24 '18 at 16:49
  • With collisions more common these days, I really like python's import mechanisms: `import pkgname` forces `pkgname.func`, which is explicit and clear; `import pkgname as pkg` allows short-cutting, which can be clear and less inconvenient, wish R had this mechanism. – r2evans Oct 24 '18 at 16:56
  • Something more basic like a real warning (not the message it currently shows) would work. – Alejandro Andrade Oct 24 '18 at 17:40
  • There are so many collisions these days and so many scripts that use `options(warn=2)` (error instead of warning), this would be a backward-incompatible change in behavior. I don't disagree, though, R's method of namespace management could use a few tweaks. – r2evans Oct 24 '18 at 17:44