1

I am trying to write a program in R that is basically a graphing calculator. It should take a text input and then plot it.

output$plot1 <- renderPlot({
    tempTxt2 <- parse(text=input$userTxtVar)
    f1 <- function(x){
      return(tempTxt2)
    }
    curve(expr = f1, from =-3, to =3)
  })

I thought parsing the text to an expr might help. It initially gives the same error, but also causes an "unexpected symbol" error. E.g. when I type in "3x":

<text>:1:2: unexpected symbol
1: 3x
     ^
manzler.h
  • 9
  • 1
  • The error comes from using `3x` instead of `3*x`. I think you will also need to `eval` `f`: e.g. change return to `f1 <- function(x){ return(eval(tempTxt2))}}` (ps this may also work: `f1 <- function(x) eval(str2expression(input$userTxtVar)); curve(f1)` – user20650 Mar 01 '23 at 16:12
  • @user20650 Thanks for the help. Simply using eval() seemed to do the trick. Do you have any advice on how to create a plot of the derivative of my function? `output$plot2 <- renderPlot({ tempTxt2 <- parse(text=input$userTxtVar) f1copy <- function(x){ return(eval(tempTxt2))} dx = deriv(f1copy, "x") f2 <- function(x){ return(dx)} curve(expr = f2, from =-3, to =3) })` – manzler.h Mar 09 '23 at 14:56
  • manzler.h; you will need to calcalue the derivative of the unevaluated function. And then `f2` needs to be `eval`ed as before. So try `dx = D(tempTxt2, "x") ; f2 <- function(x){ return(eval(dx)) }` – user20650 Mar 09 '23 at 15:11

0 Answers0