有没有办法在R Shiny应用程序中创建的对象上运行任意代码? [英] Is there a way to run arbitrary code on objects created in a R Shiny app?

查看:53
本文介绍了有没有办法在R Shiny应用程序中创建的对象上运行任意代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的用户想使用我的Shiny App创建的对象运行一些R脚本.例如.如果我的应用创建了新的数据框,他们希望使用新的数据框运行自己的分析.

My users would like to run some R scripts using the objects that my Shiny App creates. E.g. if my app creates a new data frame, they would like to run their own analysis using the new data frame.

有没有办法做到这一点?也许R Shiny中有一些类似控制台的功能(交互式)?

Is there a way to do that? Maybe some console-like (interactive) feature in R Shiny?

我发现此在运行闪亮的应用程序,但是想知道除了构建自己的服务器之外,还有其他方法可以实现吗?

I found this Access/use R console when running a shiny app, but wondering if there is any other way to do it besides building your own server.

非常感谢任何输入.谢谢!

Any input is great appreciated. Thank you!

推荐答案

如果要在运行应用程序后使数据框在全局环境中对用户可用,则可以使用 assign().以下示例使用了可以作为 RStudio插件添加的闪亮小部件的逻辑.:

If you want to make a data frame available to the user in the global environment after running the app, you can use assign(). The following example uses the logic of a shiny widget that can be added as an add-in to RStudio:

shinyApp(
  ui = fluidPage(
    textInput("name","Name of data set"),
    numericInput("n","Number observations", value = 10),
    actionButton("done","Done")
  ),
  server = function(input, output, session){
    thedata <- reactive({
      data.frame(V1 = rnorm(input$n),
                 V2 = rep("A",input$n))
    })

    observeEvent(input$done,{
      assign(input$name, thedata(), .GlobalEnv)
      stopApp()
    })
  }
)

请记住,尽管您的R线程在闪亮的应用程序运行时会连续执行,所以您只有在应用程序停止运行后才能访问全局环境.这是具有闪亮界面的程序包如何处理的.

Keep in mind though that your R thread is continuously executing when a shiny app is running, so you only get access to the global environment after the app stopped running. This is how packages with a shiny interface deal with it.

如果您希望用户在应用程序运行时能够使用该数据框,则可以使用

If you want users to be able to use that data frame while the app is running, you can add a code editor using eg shinyAce. A short example of a shiny App using shinyAce to execute arbitrary code:

library(shinyAce)
shinyApp(
  ui = fluidPage(
    numericInput("n","Number observations", value = 10),
    aceEditor("code","# Example Code.\n str(thedata())\n#Use reactive expr!"),
    actionButton("eval","Evaluate code"),
    verbatimTextOutput("output")
  ),
  server = function(input, output, session){
    thedata <- reactive({
      data.frame(V1 = rnorm(input$n),
                 V2 = rep("A",input$n))
    })

    output$output <- renderPrint({
      input$eval
      return(isolate(eval(parse(text=input$code))))
    }) 
  }
)

但是该软件包附带了一些不错的示例,因此也请看一看.

But the package comes with some nice examples, so take a look at those as well.

这篇关于有没有办法在R Shiny应用程序中创建的对象上运行任意代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆