I am trying to build an interactive scatter plot using brilliant. Using aperture data, I would like the user to select the x and y sizes of the scatter plot * petal vs sepal), and then display a simple scatter plot of the selected measurements. Pretty simple.
First I needed to create a function that allows me to pass strings representing dimensions to ggplot. I did this and tested it with static data. It works great.
Next, I define two drop-down lists and two subsequent lines (using shiny ones) for the sizes of the petals and sepals (these are my x and y axes).
Then I set the two string variables using the shiny reactive () function using the switch statement.
Everything seems to go wrong.
The error I get is: Error: cannot force type "closure" to a vector of type "symbol"
I took a few steps to debug my code. I first connected the hard-coded sizes (for example, "Petal.Length") to the final line of my code output $ myplot = renderPlot ({myplotfunct (...
This works great. The plot displays as I expect.
Then I added a debug line to track the value of the line through which I pass this plot function. Bingo. He is empty. Why is it empty? It looks like this should be passed the value from the UI.r. file
The code is below.
Any help would be greatly appreciated. Thanks!
UI.R
library(shiny) # Define UI for dataset viewer application shinyUI(fluidPage( # Application title titlePanel("Shiny Text"), # Sidebar with controls to select a dataset and specify the # number of observations to view sidebarLayout( sidebarPanel( selectInput("dataset1", "Choose a Sepal Measure:", choices = c("Sepal Length", "Sepal Width")), selectInput("dataset2", "Choose a Petal Measure:", choices = c("Petal Length", "Petal Width")) ), # Main Scatter Plot mainPanel( textOutput("testvar"), plotOutput("myplot") ) ) ))
Server.R
library(shiny) library(datasets) library(ggplot2) #Define a function to plot passed string variables in ggplot myplotfunct = function(df, x_string, y_string) { ggplot(df, aes_string(x = x_string, y = y_string)) + geom_point() } shinyServer(function(input, output) { # Sepal Inputs datasetInput1 <- reactive({ switch(input$dataset1, "Sepal Length" = "Sepal.Length", "Sepal Width" = "Sepal.Width") }) # Petal Inputs datasetInput2 <- reactive({ switch(input$dataset2, "Petal Length" = "Petal.Length", "Petal Width" = "Petal.Width") }) #Debug print value of sting being passed output$testvar = renderText(print(datasetInput1)) # Plot output$myplot = renderPlot({myplotfunct(iris, datasetInput1, datasetInput2)}) })