pacomet
pacomet

Reputation: 5141

R shiny reactive data subset

With the help of fantastic people here at Stackoverflow I've managed to build a shiny web app (thanks to shiny server developers) that lets me select the dataset to use and plots a nice table showing the complete dataset. Now I want the user to input a date range then to show the table for the data only in the range by pressing a submit button.

When the request is submitted an error message appears:

DataTables warning (table id = 'DataTables_Table_0'): DataTables warning: JSON data from server could not be parsed. This is caused by a JSON formatting error.

I think this is not an R error. I've found some info on JSON googleing but it was related to Javascript, which I dont't know how to manage. I am using Firefox to test the app.

Please find here the code I'm using for ui.R

library(shiny)

shinyUI(pageWithSidebar(
  # Título superior
  headerPanel(""),
  # Panel lateral izquierdo - selección de datos
  sidebarPanel(
    selectInput("torre", "Torre:",
                list("Place 1" = "place1",
                     "Place 2" = "place2")),       
    selectInput("tipo", "Intervalo de datos",
                list("Daily" = "-daily.csv",
                     "Monthly" = "-monthly.csv")),
    textInput("date1", "Fecha inicial:", "2000-01-01"),
    textInput("date2", "Fecha final:", "2000-01-01"),
    submitButton("Update View")
  ),

  # Panel principal (presentación de gráficas)
  mainPanel(
    tabsetPanel(
      tabPanel("Inicio",
               h3("Consulta de datos"),
               p(HTML("En el panel <i>Ayuda</i> se describen las variables 
                     presentadas en la tabla de datos.")),
               tableOutput("view")
               ),
      tabPanel("Ayuda", htmlOutput("ayuda"),id="ayuda"),
      tabPanel('Tabla de datos', dataTableOutput("mytable3"))      
    )
  )
))

and 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({
    paste0(input$date1," 00:00:00")
  })
  day2=reactive({
    paste0(input$date2," 23:50:00")
  })
  # Lectura de datos  
    datos2=reactive({
    read.csv(filename(),header=T, sep=",",na.strings="-99.9")
  })
  datos=reactive({
    d1 <- as.POSIXct(day1())
    d2 <- as.POSIXct(day2())
    with( datos2() , datos2()[ date >= d1 & date <= d2, ] )
  })
  #  Tabla de datos
  output$mytable3 = renderDataTable({
    datos()
  }, options = list(aLengthMenu = c(10, 25, 50), iDisplayLength = 10))
})

Thanks for your help

EDIT1

Also tried this piece of code in server.R

  datos=reactive(function(){
    subset(datos2(),datos2()$date >= day1())
    subset(datos2(),datos2()$date <= day2())
  }) 

and some variation like

  datos=reactive(function(){
    subset(datos2(),date >= input$date1)
    subset(datos2(),date <= input$date2)
  })

without succes. In both cases data table shows whole dataset, it seems to run after changing input dates but subsetting does not take effect.

EDIT 2

Data reupload since ubuntu one link was broken. Please, check the data for headers and formats as the original data were lost and maybe there were some changes.

You can find data here monthly and daily

Upvotes: 1

Views: 6516

Answers (1)

Dieter Menne
Dieter Menne

Reputation: 10215

In the files you uploaded, only place2-daily.csv is valid. place1-daily has a corrupt header (check with Notepad), and the monthly do not have a day column as required by your subset.

So let's fix the filename:

  datos2=reactive({
    #fn = filename()
    fn = "place2-daily.csv"
    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, ] )
  })

You date is read in as a factor, so you have to convert it first. And in the datos part, I recommend to assign to a local variable. My rule of thumb is to always assign whenever a () is called more than once.

Upvotes: 3

Related Questions