r在renderTable和renderDataTable之间显示不同的输出 [英] R shiny different output between renderTable and renderDataTable

查看:31
本文介绍了r在renderTable和renderDataTable之间显示不同的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个闪亮的应用程序,它使用renderDataTable显示我的数据的漂亮html表。然后我想做一些基本的统计、子集数据和计算方法以及其他一些数据。

使用renderTable显示结果时,我发现日期列没有以日期格式显示。在图中你可以看到不同之处。这两个表都是从同一闪亮Web应用程序中的同一数据集生成的。你能解释一下发生了什么事吗?

在这里您可以看到ui.R和server.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"))
    )
  )
))

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)
    }
  )

})

提前感谢您的帮助

推荐答案

没有理由从两个不同的函数获得相同的输出。

  • rendertable使用xtable创建html表
  • renderDataTable使用外部javascript库
您可以获得相同的日期格式,只需在调用renderTable之前格式化日期即可。下面是一个简短的完整示例。

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

这篇关于r在renderTable和renderDataTable之间显示不同的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆