Sandy Ross
Sandy Ross

Reputation: 33

How do I get my data to render correctly?

Could somebody review my code to make sure its all correct? I have read the data in and looks all fine, just when I go to run the shiny, the chart appears, but no data. I have hidden some functionality from the program with the # as I couldn't get additional variables to select correctly. I have included some data for the weather_files, weather_years and weather data.

weather_files

structure(list(Town = structure(c(2L, 4L, 2L, 1L, 1L, 1L, 1L, 
4L, 1L, 4L, 5L, 1L, 2L, 4L, 2L, 5L, 5L, 1L, 3L, 4L, 3L, 3L, 5L, 
5L, 5L), .Label = c("braemar", "dunstaffnage", "nairn", "stornoway", 
"tiree"), class = c("ordered", "factor")), year = structure(c(132L, 
37L, 128L, 100L, 137L, 147L, 143L, 22L, 103L, 89L, 96L, 138L, 
109L, 87L, 142L, 109L, 61L, 138L, 136L, 75L, 112L, 92L, 118L, 
100L, 66L), .Label = c("1873", "1874", "1875", "1876", "1877", 
"1878", "1879", "1880", "1881", "1882", "1883", "1884", "1885", 
"1886", "1887", "1888", "1889", "1890", "1891", "1892", "1893", 
"1894", "1895", "1896", "1897", "1898", "1899", "1900", "1901", 
"1902", "1903", "1904", "1905", "1906", "1907", "1908", "1909", 
"1910", "1911", "1912", "1913", "1914", "1915", "1916", "1917", 
"1918", "1919", "1920", "1921", "1922", "1923", "1924", "1925", 
"1926", "1927", "1928", "1929", "1930", "1931", "1932", "1933", 
"1934", "1935", "1936", "1937", "1938", "1939", "1940", "1941", 
"1942", "1943", "1944", "1945", "1946", "1947", "1948", "1949", 
"1950", "1951", "1952", "1953", "1954", "1955", "1956", "1957", 
"1958", "1959", "1960", "1961", "1962", "1963", "1964", "1965", 
"1966", "1967", "1968", "1969", "1970", "1971", "1972", "1973", 
"1974", "1975", "1976", "1977", "1978", "1979", "1980", "1981", 
"1982", "1983", "1984", "1985", "1986", "1987", "1988", "1989", 
"1990", "1991", "1992", "1993", "1994", "1995", "1996", "1997", 
"1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005", 
"2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013", 
"2014", "2015", "2016", "2017", "2018", "2019", "2020"), class = 
"factor"), 
month_year = structure(c(10L, 7L, 2L, 7L, 6L, 11L, 8L, 3L, 
4L, 3L, 12L, 6L, 1L, 7L, 3L, 9L, 4L, 4L, 8L, 4L, 10L, 3L, 
4L, 10L, 8L), .Label = c("April", "August", "December", "February", 
"January", "July", "June", "March", "May", "November", "October", 
"September"), class = "factor"), month = c(11, 6, 8, 6, 7, 
10, 3, 12, 2, 12, 9, 7, 4, 6, 12, 5, 2, 2, 3, 2, 11, 12, 
2, 11, 3), `Maximum Temp` = c(104, 134, 191, 132, 178, 98, 
76, 8, 6, 58, 151, 17, 121, 158, 81, 132, 71, 23, 82, 29, 
93, 59, 81, 93, 101), `Minimum Temp` = c(63, 8, 115, 5, 82, 
2, 1, 26, 22, 11, 101, 89, 36, 87, 28, 75, 31, 58, 16, 21, 
42, 1, 31, 41, 66), `Air Frost Days` = c(2, 0, 0, 0, 0, 12, 
13, 4, 17, 19, 0, 0, 2, 0, 3, 0, 2, 24, 6, 17, 2, 12, 2, 
1, 0), `Rainfall (mm)` = c(1074, 564, 107, 636, 884, 642, 
684, 1789, 356, 104, 1281, 93, 247, 744, 2712, 78, 904, 156, 
416, 111, 841, 466, 2463, 1104, 1091), Sunshine = c(NA, NA, 
1538, 1431, NA, NA, NA, NA, 801, 263, 1375, NA, NA, 206, 
NA, 1993, 723, NA, 1068, 863, 339, NA, 459, 456, 628)), row.names = 
c(NA, 
-25L), class = c("tbl_df", "tbl", "data.frame"))

Ones that read it, the input data for the Shiny is this:

weather_data <- levels(weather_files$Town)
Weather_years <- levels(weather_files$year)
Weather_month <- levels(weather_files$month_year)

Essentially what im trying to do is select the Town, then the Year and a variable (columns 5:9 - however that wont work at all), so i chose Sunshine column instead for now. Then plot the month_year column on the X axis against the Sunshine column on Y axis, colour/ fill by year as id hope you can select multiple years. Hope that makes what im doing clear. Thanks in advance.

ui <- fluidPage(

 titlePanel("Met Office Weather"),


sidebarLayout(
sidebarPanel(
  selectInput(label = ("Select Data Set"),
              inputId = "Town",
              choices = weather_data,
              multiple = TRUE),
  
  selectInput(label = "Select Year",              
              inputId = "year",
              choices = Weather_years,
              multiple = TRUE),
  
  #selectInput(inputId = "selection",
      #             label="Select Variable",
       #             choices = c("Sunshine"),
        #           multiple = FALSE),
  
  actionButton(inputId = "go", label = "Plot Data")
),

# Show the generated plot
mainPanel(
  tabsetPanel(
    tabPanel("scatterplot", plotOutput("scatter")),
    tabPanel("Barplot", plotOutput("barplot")),
    tabPanel("The Data", dataTableOutput("table"))
   )
  )
 )
)


server <- function(input, output) {

select_weatherdf <- reactive({
# gather info from user but only when asked
pick_town <- isolate(input$Town)
select_year <- isolate(input$year)
#choose_variable <- isolate(input$Sunshine)

# listen to go button
input$go

# select the correct town and year looking at sunshine variable only
weather_to_plot <- weather_files %>% filter(Town %in% pick_town, year %in% select_year)
#weather_to_plot <- weather_to_plot %>% select("Town", "year", "month_year", "Sunshine")

return(weather_to_plot)

})

 output$scatter <- renderPlot({

 data_to_plot <- select_weatherdf()
 ggplot(data_to_plot, aes(x = month_year, y = Sunshine , fill = year)) +
  geom_point(size = 2.5) +
  scale_x_discrete(limits = month.name)



 })

  output$barplot <- renderPlot({

data_to_plot <- select_weatherdf()
ggplot(data_to_plot, aes(x = month_year, fill = year)) + 
  geom_bar() +
  scale_x_discrete(limits = month.name)
 })


   output$table <- renderDataTable({

data_to_plot <- select_weatherdf()

})
}

# Run the application 
shinyApp(ui = ui, server = server)

Upvotes: 3

Views: 68

Answers (1)

jpdugo17
jpdugo17

Reputation: 7106

The app is working as intended, the reason that there's nothing in the plots it's because the data has some NA's in the sunshine col and the choices for the inputs are based in the levels of the variables but not the unique values from it. All that combined will yield choices in the selectInput's that will not be present in the plot. Instead try doing:

weather_data <- unique(weather_files$Town)
Weather_years <- unique(weather_files$year)
Weather_month <- unique(weather_files$month_year)

This will narrow the choices of the inputs to data that is observed in the data set.

Optionally we can create the reactive select_weatherdf like this:

  select_weatherdf <- eventReactive(input$go, {
   weather_files %>% filter(Town %in% input$Town, year %in% input$year) %>% 
     select("Town", "year", "month_year", "Sunshine")
    #no need for a return statement here R will return the last value called from the function
  })

Upvotes: 1

Related Questions