R Shiny终止后将输出保存到文件 [英] Save output to file after R shiny terminates

查看:192
本文介绍了R Shiny终止后将输出保存到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有启动R闪亮应用程序的功能,允许用户选择各种颜色.

I have function that launches R shiny app, allowing users to select various colors.

但是如果用户改变主意并取消选择颜色怎么办.

But what if user changes their mind and deselects a color.

因此,我希望在R Shiny终止后将用户输出保存到文件中.

Hence I wish to save user output to file after R shiny terminates.

但是,每次启动闪亮时,文件都会重置,以便可以接收新信息.

However, each time shiny is launched, the file resets so it can take in new information.

尝试了session$onSessionEnded,但在执行时却给出了错误

Tried session$onSessionEnded, but it gives error upon execution

Listening on http://127.0.0.1:7431
Warning: Error in .getReactiveEnvironment()$currentContext: Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)
  42: stop
  41: .getReactiveEnvironment()$currentContext
  40: .dependents$register
  39: outuputdata
  37: callback [c:\RanglaPunjab/R/RanglaPunjab.R#237]

下面是代码和示例输入.这是整个R脚本

Below is code and sample input. This is entire R script

CherryPickPalette <- function (name, name2=NULL, name3=NULL){

  if ((nargs() < 2) || (nargs() > 3)){
    stop("Enter 2 or 3 valid palettes. Run ListPalette() for list of palettes.")
  }
  if (nargs() == 2){
    new_pal <- MergePalette(name,name2)
  }
  else if (nargs() == 3){
    new_pal <- MergePalette(name,name2,name3)
  }

  if (interactive()){
    colorfile <- paste(getwd(),"colorfile.txt",sep="/")
    if (!file.exists(colorfile)){
      file.create(colorfile)
    }
    shinyApp(
      ui = fluidPage(
        titlePanel("Cherry Pick Your Own Palette!"),
        sidebarPanel (hr(),
                      selectInput('col', 'Options', new_pal, multiple=TRUE, selectize=FALSE, size = 15)
                      ),
        mainPanel(
          h5('Your custom colors',style = "font-weight: bold;"),
          fluidRow(column(12,verbatimTextOutput("col"))))
      ),
      server = function(input,output,session){
        outuputdata<-  reactive({
          input$col
        })

        output$col <- { 
          renderPrint(outuputdata())
        }
        session$onSessionEnded(function(){
          message <- paste(outuputdata(),"\n")
          cat(message,file=colorfile, append=TRUE)
        })
      }

    )
  }
}

CherryPickPalette("BiryaniRice","Kulfi","Haveli2")

推荐答案

您必须使用isolate来访问反应式上下文之外的反应式值. 以下对我有用

You have to use isolate to access reactive values outside of a reactive context. The following worked for me

    session$onSessionEnded(function(){
      message <- paste(isolate(outuputdata()),"\n")
      cat(message,file=colorfile, append=TRUE)
    })

这篇关于R Shiny终止后将输出保存到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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