0

I want to create a shiny app interpolating weather data. When I try to access my data inside a reactive expression I always get as a result NULL. I don't know where I am wrong. I tried to imitate my problem as simple as possible. Here we go:

My datatable:

lon lat Temperature AirPressure
50.0 25.0 12.1 1012.0
51.0 26.0 13.4 1011.0

name of the datatalbe: myDataTable

My ui.R:

selectInput(inputId = "getData",
                label = "used Data:",
                choices = c("Temperature","AirPressure"),
                selected = "Temperature"
)

Inside my reactive expression I want to access the data depending on what is selected. Now when I do the following:

1. Test

print(myDataTable$Temperature)

Result (as expected): [1] 12.1 13.4

2. Test

print(myDataTable$input$getData)

Result (not as expected): NULL

3. Test

tempVal <- input$getData
print(tempVal)
print(myDataTable$tempVal)

Result (not as expected):

[1]Temperature

NULL

obrob
  • 843
  • 1
  • 8
  • 20

1 Answers1

0

It seems you're subsetting the data incorrectly, here are the 3 tests you have within the observe statement so you can see the output

#rm(list = ls())
library(shiny)

lon <- c(50,51)
lat <- c(25,26)
Temperature <- c(12.1,13.4)
AirPressure <- c(1012,1011)

myDataTable <- data.frame(cbind(lon,lat,Temperature,AirPressure))

ui <- fluidPage(
  selectInput(inputId = "getData",label = "used Data:",
              choices = c("Temperature","AirPressure"),selected = "Temperature"
  )
)

server <- function(input, output) {

  observe({
    # Test 1
    print(myDataTable$Temperature)
    # Test 2
    print(myDataTable[,names(myDataTable) %in% input$getData])
    # Test 3
    tempVal <- input$getData    
    print(myDataTable[,names(myDataTable) %in% tempVal])
  })
}
shinyApp(ui = ui, server = server)

enter image description here

Pork Chop
  • 28,528
  • 5
  • 63
  • 77
  • ok, very interesting: When I use your syntax as following: "myDataTable[,names(myDataTable) %in% input$getData]" it works. Mine doesn't ... – obrob Dec 02 '16 at 10:51
  • note that the `selectInput` is an object so you have to use it as such – Pork Chop Dec 02 '16 at 10:52
  • I don't really understand the syntax. Never used this before.Where can I find some information how to read it? Are there some tags I can look for to understand whats happening here. – obrob Dec 02 '16 at 11:38
  • this is normal `r` syntax have a look at examples here http://stackoverflow.com/questions/10085806/extracting-specific-columns-from-a-data-frame – Pork Chop Dec 02 '16 at 12:59