eval(parse(text=“f”)) on shinypass.io

China☆狼群 提交于 2020-01-17 05:01:06

问题


I want to write an app that lets the user enter a function using textInput, and then does something with the function. Here is a toy example:

shinyUI(fluidPage(
   titlePanel("Test"),
  sidebarLayout(
     sidebarPanel(
        textInput("fun","function:",value="x")
    ),
  mainPanel(
      uiOutput("text")
  )
)

shinyServer(function(input, output) {
  findMax <- reactive({
       f <- function(x) eval(parse(text = input$fun), envir = list(x))
       x < seq(0,1,length=100)
       max(f(x)) 
   })  
   output$text <- renderText( {   
       findMax()
    })     

  })
))

and this works just fine when run on my computer locally. But when i submit it to shinyapps.io i get the error: object x not found. It seems there is a problem with the envir argument of eval, but i have not been able to find out what it is.

There is of course a lot of discussion on the eval(parse()) construct in general, so if anyone has a suggestion on how to do this (have the ability to type in an expression in a box and get it turned into a function) differently i would also be grateful.

Wolfgang


回答1:


After trying a number of things i finally got this one to work:

instead of

f <- function(x) eval(parse(text = input$fun), envir = list(x))

use

eval(parse(text = paste("f <- function(x)",input$fun,sep="")))

i have no idea why both work on my computer locally but only the second works on shinyapps.io . Also, i would still be interested if anyone has a different way to do this.

Wolfgang



来源:https://stackoverflow.com/questions/34108611/evalparsetext-f-on-shinypass-io

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!