Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I render a shiny map in rmarkdown #22

Closed
pssguy opened this issue Dec 19, 2014 · 6 comments
Closed

How do I render a shiny map in rmarkdown #22

pssguy opened this issue Dec 19, 2014 · 6 comments

Comments

@pssguy
Copy link

pssguy commented Dec 19, 2014

Hi
I have no problems creating a static leafletmap in rMarkdown. However I'm not sure how to render one where there is an input variable. This approach creates the data OK but does not render map.

observe({

data <- calcs involving input$var

glimpse(data) # confirmscorrect values in console

leaflet(data) %>%
addTiles() %>%
addCircleMarkers(info based on data)

})

should I be using renderLeaflet somehow or adding a binding similar to that used for ggvis in rMarkdown

@yihui
Copy link
Member

yihui commented Dec 20, 2014

It is just like all traditional Shiny output elements, i.e. use leafletOutput(id) in ui.R, and output$id = renderLeaflet(leaflet() %>% addTiles() %>% ...) in server.R.

@yihui yihui closed this as completed in 3cfbcc1 Dec 20, 2014
@pssguy
Copy link
Author

pssguy commented Dec 20, 2014

I was hoping to code directly in Rmarkdown without the need to wrap it in an embedded shinyApp
That is my normal practice if I want to display dataTables or ggvis plots, for example
However, it is no big deal as I have gone the embedding route and seems to work fine

@yihui
Copy link
Member

yihui commented Dec 20, 2014

I think you can just replace observe() with renderLeaflet().

@jjallaire
Copy link
Member

I think you just need to add runtime: shiny to the YAML and then you can use leaflet in reactives. It would be something like this:

renderLeaflet({

   data <- calcs involving input$var

   glimpse(data) # confirmscorrect values in console

   leaflet(data) %>% 
      addTiles() %>% 
      addCircleMarkers(info based on data)

})

@yihui does that look right?

@yihui
Copy link
Member

yihui commented Dec 20, 2014

@jjallaire Exactly. Here is a minimal example:

---
output: html_document
runtime: shiny
---

Test leaflet

```{r}
library(leaflet)
library(shiny)
sliderInput('n', 'Number of markers', min = 1, max = 100, value = 30, step = 1)
renderLeaflet({
  n = input$n
  df = data.frame(lat = rnorm(n, 42), lng = rnorm(n, -93))
  leaflet(df) %>% addTiles() %>% addMarkers()
})
```

@pssguy
Copy link
Author

pssguy commented Dec 20, 2014

Thanks for assistance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants