32670

Problems using ggvis in rMarkdown with variables

Question:

Hi having a couple of problems

a) creating the correct text to pass variables to ggvis - not even sure aes_string is applicable

b) The plot propagates in browser rather than rendering in the rmarkdown document

Here is an example

--- title: "Untitled" author: "pssguy" date: "Sunday, August 24, 2014" output: html_document runtime: shiny --- ```{r, echo = FALSE, message=FALSE} library(ggplot2) library(ggvis) library(dplyr) selectInput("category3", "Choose Dataset:", c("mpg", "disp", "qsec")) # ggplot renders correctly within renderPlot renderPlot({ ggplot(mtcars,aes_string(input$category3,"disp"))+geom_point() }) # ggvis works within document with hard coded info mtcars %>% ggvis(~wt,~disp) mtcars %>% ggvis(aes_string(paste("~",input$category3,","),"~disp")) #Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.) # This needs correcting anyways renderPlot({ mtcars %>% ggvis(aes_string(paste("~",input$category3,","),"~disp")) }) # <text>:1:7: unexpected ',' 1: ~ mpg , # even if the above is corrected the plot opens in a browser rather than the document renderPlot({ mtcars %>% ggvis(~wt,~disp) }) ```

TIA

Answer1:

This should do it:

--- title: "Untitled" output: html_document runtime: shiny --- ```{r, echo = FALSE, message=FALSE} library(ggplot2) library(ggvis) library(dplyr) selectInput("category3", "Choose Dataset:", c("mpg", "disp", "qsec")) # ggplot renders correctly within renderPlot renderPlot({ print(input$category3) ggplot(mtcars,aes_string(input$category3,"disp"))+geom_point() }) # ggvis with dynamically changing columns reactive({ if (!is.null(input$category3)) col <- input$category3 else col <- "mpg" mtcars %>% ggvis(prop("x", as.name(col)), ~disp) }) %>% bind_shiny('foo') ggvisOutput('foo') ```

It's a little complicated because you need a NULL check for the category, and you need to explicitly tell knitr to put a ggvis output on the page.

Recommend

  • navbarPage shiny with two datasets and identical set of widgets - both ways dependence
  • SelectizeInput does not display all choices on shiny.io
  • Use plotlyProxy to add multiple traces when data changes
  • How to change the width of area around SelectInput in R shiny
  • Showing Monthly Totals from Multiple Columns in PostgreSQL
  • How can I add a minimum of days based on hour in datetimepicker?
  • In R, adding multiple RShiny actionButton (or selectInput) widgets to one row
  • How to get the time on the current android device?
  • Remove duplicates based on order
  • Count row with condition per group
  • jquery date conversion chrome works but IE and firefox don't
  • SpatialPolygonDataFrame plotting using ggplot
  • Why ggplot2 pie-chart facet confuses the facet labelling
  • How to plot stacked proportional graph?
  • Multiple plots on one page using ggplot
  • How to get rows with min values in one column, grouped by other column, while keeping other columns?
  • Use conditional coloring on a plotly surface
  • Laravel Carbon localization not working (get localized name of month from number)
  • boxplot won't display with ggvis
  • Retrieve list of sent friend requests from friend_request FQL table
  • debug library loaded with ctypes using gdb
  • Combining SpatialPolygonsDataFrame of two neighbour countries
  • NHibernate Validation Localization with S#arp Architecture
  • How can I send an e-mail from a vbs script
  • Accessing IRQ description array within a module and displaying action names
  • Does CUDA 5 support STL or THRUST inside the device code?
  • Modifying destination and filename of gulp-svg-sprite
  • Deserializing XML into class C#
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • Why winpcap requires both .lib and .dll to run?
  • Use group_by to filter specific cases while keeping NAs
  • Getting Messege Twice Using IMvxMessenger
  • How to stop GridView from loading again when I press back button?
  • Bitwise OR returns boolean when one of operands is nil
  • python draw pie shapes with colour filled
  • sending mail using smtp is too slow
  • reshape alternating columns in less time and using less memory
  • costura.fody for a dll that references another dll
  • Binding checkboxes to object values in AngularJs
  • How to Embed XSL into XML