0

I read this tried solution from this question, but didn't work.

CherryPickPalette is an R function that takes in 2 to 3 color palettes, allows user to select their own colors, saves results to a file, scans the results into a variable.

Unfortunately, I am unable to read the variable.

Entire R script is here

When I execute script and exit it says number of items it read (based on user selection)

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

Listening on http://127.0.0.1:4346
Read 7 items

However if I enter customcolors I get error

> customcolors
Error: object 'customcolors' not found

Below is function ... please guide.

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

  # error checking    

  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(isolate(outuputdata())," ")
          cat(message,file=colorfile, append=TRUE)
          customcolors <- scan(file=colorfile," ")

          # trying to return customcolors outside of shiny
          stopApp(customcolors)
          customcolors
          ######
        })
      }
    )
  }
}
Artie Ladie
  • 521
  • 4
  • 14

1 Answers1

2

After running and closing the app:

customcolors <- scan("colorfile.txt", character())
customcolors

# [1] "#a96337" "#f0ecb0" "#4a4000" "#001a24"

Or run the app with:

customcolors <- runApp(CherryPickPalette("BiryaniRice","Kulfi","Haveli2"))
Aurèle
  • 12,545
  • 1
  • 31
  • 49
  • Hello, These two commands work on the terminal. I'd like to fold it into the function itself, i.e. user should only call `CherryPickPalette(name, name2, name3)`.If I include `customcolors <- scan("colorfile.txt", character()) customcolors` after the app close, and just before function `CherryPickPalette()` terminates, I get error with `devtools::check()`, i.e. `Error in file(file, "r") : cannot open the connection`. Tinkering with code, kindly assist. – Artie Ladie Jun 08 '18 at 16:55
  • Or maybe I can make `CherryPickPalette` call a hidden function to launch shiny app that returns the value. Just thinking out loud....... – Artie Ladie Jun 08 '18 at 17:00
  • Your answer helps for general purposes, will post an updated question soon. – Artie Ladie Jun 08 '18 at 18:27
  • Please answer this if possible, https://stackoverflow.com/questions/50767666/call-r-shiny-app-with-argument – Artie Ladie Jun 08 '18 at 20:01