0

I have a shiny app that creates a ggplot for a given input. Now I want to give the user the freedom to save the ggplot including additional commands by the means of a textInput. When I use eval(input$text_input) (to evaluate the expression in the text input "text_input") I get an error.

A MWE looks like this:

library(shiny)
library(ggplot2)

ui <- shinyUI(
  fluidPage(
    plotOutput("plot1"),
    textInput("text_input", "Additional Args", "scale = 0.5"),
    actionButton("save", "Save")
  )
)

server <- function(input, output) {
  df <- data.frame(x = 1:10)
  p1 <- ggplot(df, aes(x, x)) + geom_point()

  output$plot1 <- renderPlot(p1)

  observeEvent(input$save, {
    ell <- input$text_input # for example ell <- "scale = 0.5"
    # This code returns the error
    ggsave(filename = "plot1.pdf", p1, ... = eval(ell)) 

    # TARGET:
    # ggsave(filename = "plot1.pdf", p1, scale = 0.5)
    # or whatever the user tries to put into the text_input
  })
}

shinyApp(ui, server)

Do you have any ideas on how to fix the evaluation within the ggsave function?

Edit/Solution

Thanks to @warmoverflow, this line fixes the issue:

eval(parse(text = paste("ggsave(filename = 'plot1.pdf', p1,", ell, ")")))
David
  • 9,216
  • 4
  • 45
  • 78
  • 1
    You need to `eval` the whole command, not just the input text. See http://stackoverflow.com/questions/7836972/use-character-string-as-function-argument – Xiongbing Jin Mar 30 '16 at 14:28
  • Thanks, that did it! If you post it as a solution, I will gladly accept it as an answer! – David Mar 30 '16 at 14:37

0 Answers0