2

I am building a dashboard for a colleague and she requested that in the dashboard I should display live weather for a city. I looked online and I saw some really simple code for weather from the dark sky here. It was embedded in an iframe. I wanted to do exactly this but the dark sky is not handing out any more API keys. So I found online another weather source called open weather I got an API key and I tried out the following code.

    tabItem(tabName = 'weather',
      fluidRow(
        tags$iframe(
          seamless = "seamless",
          src = "http://api.openweathermap.org/data/2.5/forecast?id=2761333&APPID= my key",
          height = 800,
          width = 1400
        )
      )
    )

I didn't get any error message or anything but in the dashboard, you just see something like this

{"cod":"200","message":0,"cnt":40,"list":[{"dt":1596456000,"main":{"temp":293.37,"feels_like":293.3,"temp_min":293.37,"temp_max":293.86,"pressure":1009,"sea_level":1009,"grnd_level":987,"humidity":79,"temp_kf":-0.49},"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"clouds":{"all":59},"wind":{"speed":3.19,"deg":206},"visibility":10000,"pop":0.87,"rain":{"3h":1.77},"sys":{"pod":"d"},"dt_txt":"2020-08-03 12:00:00"},{"dt":1596466800,"main":

Just basically data. I thought this would be a long shot anyways but I saw on the website that they have a widget constructor and they give me what looks like to me as HTML code. Here it is below:

<div id="openweathermap-widget-21"></div>
<script src='//openweathermap.org/themes/openweathermap/assets/vendor/owm/js/d3.min.js'></script><script>window.myWidgetParam ? window.myWidgetParam : window.myWidgetParam = [];  window.myWidgetParam.push({id: 21,cityid: '2761367',appid: 'my key',units: 'metric',containerid: 'openweathermap-widget-21',  });  (function() {var script = document.createElement('script');script.async = true;script.charset = "utf-8";script.src = "//openweathermap.org/themes/openweathermap/assets/vendor/owm/js/weather-widget-generator.js";var s = document.getElementsByTagName('script')[0];s.parentNode.insertBefore(script, s);  })();</script>

This should somehow make a widget that looks like this from the website here but I am not a developer I just work with R and shiny dashboards are new to me, so I am not sure how I could put this widget that displays daily weather in shiny. If Anybody can tell me how to do this or another way I would appreciate this.

KarthikNayak98
  • 363
  • 3
  • 13
Nick
  • 369
  • 1
  • 3
  • 18

1 Answers1

0

You can't use an iframe.

I tried to use an iframe with srcdoc as a sandbox around the widget, but unfortunately the openweathermap javascript doesn't like that. I sent them an email to see if they could fix that, but we'll have to wait and see.

In the meantime, all you can do is use the widget directly in your HTML. If I hear anything new I'll update here.

Emmanuel
  • 16,791
  • 6
  • 48
  • 74