简体   繁体   English

R渲染renderTable和renderDataTable之间的不同输出

[英]R shiny different output between renderTable and renderDataTable

I have a shiny application that shows a nice html table of my data, using renderDataTable . 我有一个闪亮的应用程序,使用renderDataTable显示我的数据的一个很好的html表。 Then I wanted to make some basic statistics, subset data and calculate means and some other data. 然后我想做一些基本的统计,子集数据和计算方法以及其他一些数据。

When showing results with renderTable , I found that date column was not shown in date format. 使用renderTable显示结果时,我发现日期列未显示日期格式。 In the figure ou can see the difference. 在图中你可以看到差异。 Both tables are generated from the same dataset in the same shiny web app. 两个表都是从同一个闪亮的Web应用程序中的相同数据集生成的。 Can you explain what's happening? 你能解释一下发生了什么吗?

在此输入图像描述

And here you can see ui.R and server.R. 在这里你可以看到ui.R和server.R。 In this script I just want to show a table and was surprised by the different output. 在这个脚本中我只想显示一个表,并对不同的输出感到惊讶。

ui.R ui.R

library(shiny)

# Estructura de la página (paneles)
shinyUI(pageWithSidebar(
  # Título superior
  headerPanel(""),
  # Panel lateral izquierdo - selección de datos
  sidebarPanel(
    helpText("Selecciona las fechas y el tipo de datos.
             Pulsa el botón Actualizar."),    
    selectInput("torre", "Torre:",
                list("Agres" = "mariola",
                     "Alfàs del Pi" = "shelada",
                     "Altura" = "altura",                                          
                     "Vistabella del Maestrat" = "vistabella",
                     "Xàtiva" = "xativa")),       
    selectInput("tipo", "Intervalo de datos",
                list("Diezminutales" = "-datos-10m.csv",
                     "Diarios" = "-datos-diarios.csv",
                     "Mensuales" = "-datos-mensuales.csv")),
    dateInput('date1',
              label = 'Fecha inicial',
              value = Sys.Date()),
    dateInput('date2',
              label = 'Fecha final.',
              value = Sys.Date()),
    submitButton("Actualizar"),
    helpText("
             Descarga de datos tabulados en formato CSV."),
    downloadButton('downloadData','Descargar datos')    
  ),

  # Panel principal (presentación de gráficas)
  mainPanel(
    tabsetPanel(
      tabPanel("Inicio",
               h3("Consulta de datos"),
               p(HTML("Some info.")),
               tableOutput("view")
               ),
      tabPanel("Ayuda", htmlOutput("ayuda"),id="ayuda"),
      tabPanel('Tabla de datos', dataTableOutput("table1")),      
      tabPanel('Medias', tableOutput("table2"))
    )
  )
))

and server.R server.R

library(shiny)
library(plyr)
library(lubridate)
library(scales)

options(shiny.transcode.json = FALSE)

# Funciones
shinyServer(function(input,output){

  filename=reactive({
    paste0(input$torre,input$tipo)
    })
  day1=reactive({
    as.POSIXct(input$date1)
  })
  day2=reactive({
    as.POSIXct(input$date2)    
  })

  # Ayuda
  introFile <- './ayuda.txt'
  ayuda <- readChar(introFile, file.info(introFile)$size)
  output$ayuda <- renderText({HTML(ayuda)})

  datos2=reactive({
    fn = filename()
    f = read.csv(fn,header=T, sep=",",na.strings="-99.9")
    f$date = as.Date(f$date)
    f
  })
  datos=reactive({
    d1 <- as.Date(day1())
    d2 <- as.Date(day2())
    datos2a = datos2()
    with( datos2a , datos2a[ date >= d1 & date <= d2, ] )
  })  

  #  Tabla de datos
  output$table1 = renderDataTable({
    datos()
  }, options = list(aLengthMenu = c(12, 20, 40), iDisplayLength = 12))

  output$table2 = renderTable({
    datos()
  })

  output$downloadData <- downloadHandler(
    file = c('data.csv'),
    content = function(file) {
      write.csv(datos(), file)
    }
  )

})

Thanks in advance for your help 在此先感谢您的帮助

There is no reason to have the same output from 2 different functions. 没有理由从2个不同的功能获得相同的输出。

  • rendertable uses xtable to create the html table rendertable使用xtable创建html表
  • renderDataTable uses external javascript library renderDataTable使用外部JavaScript库

You can get the same date format, you should just format your date before calling renderTable . 您可以获得相同的日期格式,您应该在调用renderTable之前格式化日期。 Here a short complete example. 这是一个简短的完整例子。

library(shiny)
dat <- data.frame(date=seq.Date(Sys.Date(),by=1,length.out=5),
                  temp = runif(5))

ui <- fluidPage(
    fluidRow(column(4,dataTableOutput('dto')),
             column(4,tableOutput('to')))
  )

server <- function(input,output){

  output$dto <- renderDataTable({dat})
  dat$date <- format(dat$date,'%Y-%m-%d')
  output$to <- renderTable(dat)

}

runApp(list(ui=ui,server=server))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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