简体   繁体   中英

DT tables not shown in shiny

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.

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(...) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM