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, ")")))