Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R renderTable和renderDataTable之间的输出不同_R_Shiny_Shiny Server - Fatal编程技术网

R renderTable和renderDataTable之间的输出不同

R renderTable和renderDataTable之间的输出不同,r,shiny,shiny-server,R,Shiny,Shiny Server,我有一个闪亮的应用程序,它使用renderDataTable显示了一个漂亮的html数据表。然后我想做一些基本的统计,子集数据,计算平均值和其他一些数据 当使用renderable显示结果时,我发现日期列没有以日期格式显示。从图中你可以看出区别。这两个表都是从同一web应用程序中的同一数据集中生成的。你能解释一下发生了什么事吗 在这里您可以看到ui.R和server.R。在这个脚本中,我只想显示一个表,并对不同的输出感到惊讶 ui.R library(shiny) # Estructura

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

当使用
renderable
显示结果时,我发现日期列没有以日期格式显示。从图中你可以看出区别。这两个表都是从同一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"))
    )
  )
))
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)
    }
  )

})
server.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"))
    )
  )
))
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)
    }
  )

})
库(闪亮)
图书馆(plyr)
图书馆(lubridate)
图书馆(比例尺)
选项(shinny.transcode.json=FALSE)
#职能
shinyServer(功能(输入、输出){
文件名=被动({
paste0(输入$torre,输入$tipo)
})
第1天=无功({
as.POSIXct(输入$date1)
})
第2天=无功({
as.POSIXct(输入$date2)
})
#阿尤达

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

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

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))
库(闪亮)

dat为什么您希望
renderDataTable
renderTable
的输出不一样?它们完全不同。然后,您应该能够通过一个小的闪亮示例重现相同的行为(不需要所有其他UI组件/服务器功能)。您有一个很长时间的不可复制的示例。@agstudy我期望得到相同的输出,因为我认为它们只是显示数据的方式,以前不知道我必须格式化数据。我假设
renderable
知道日期是某种日期格式,因为
f$date=as.date(f$date)
谢谢你的回答。@agstudy下次我会尽力处理可复制的示例。谢谢。请注意,renderTable不能很好地处理日期。你需要将它们转换为字符。例如,这是@Vincent的重点。在这种情况下,我将在处理日期时使用
renderDataTable
。谢谢