2
votes

I am trying to create a small shiny app using R-markdown. My app lookes like:

---
title: "MySHinyApp"
author: "Eeshan Chatterjee"
date: "Wednesday 25 March 2015"
output: html_document
runtime: shiny
---

```{r, echo=FALSE}
source("~/MyAnalysis.R")


inputPanel(textInput("filepath",label = "Full path to appnexus csv export file",value = "~/Downloads/rawData.csv"))
renderText(paste0("Reading file: ",input$filepath))

mydata = reactive(
  tryCatch(read.csv(text=readLines(input$filepath),header = T),
           error = function(e){
             return(matrix('Placeholder',1,1))
           })

)

# renderText(paste(mydata()[1,],collapse = ','))
renderText("=====================================")

plotData = reactive({
  analysis = analyseData(mydata())
  return(analysis)
  })
input_list = reactive(list(names(plotData()$inputList)))


inputPanel(selectInput("ip_selection",label = "Select Input",choices = input_list(),selected = input_list()[1]))
renderText(input$ip_selection)
```

MyAnalysis.R looks like:

analyseData = function(data){
  # Do some analysis
  # ....
  #Give output in the format:
  analysedData = list(inputList = list("a","b","c"),otherMetrics = list(one="one"))
  # "a","b","c" come from the data, essentially subsetting/modelling parameters. Can't be static, these are data-specific
  return(analysedData)
}

and myData looks like:

1,2,3,4,5,6
3,9,4,8,7,5
4,2,8,4,9,6

When I run this, I get the following Error:

Error: Operation not allowed without a reactive context. (You tried to do something that can only be done from inside a reactive expression or observer)

I'm not sure where I'm going wrong and how to fix it. Any help appreciated!

1

1 Answers

3
votes

You are trying to create ui based on values selected, you forgot to apply basics of shiny here. If UI is generated based on server side values you have to create it using renderUI.

I hope following will work for you.. I checked it my end, built properly and page started well. I dont know input file and expected values.

---
title: "MySHinyApp"
author: "Eeshan Chatterjee"
date: "Wednesday 25 March 2015"
output: html_document
runtime: shiny
---

```{r, echo=FALSE}
library(shiny)
source('MyAnalysis.R')

inputPanel(
  fileInput("filepath", label = "Full path to appnexus csv export file"#, value = "~/Downloads/rawData.csv"
    ))

mydata = reactive({
  df = tryCatch(read.csv(text=readLines(input$filepath),header = T),
                error = function(e){
                  return(matrix('Placeholder',1,1))
                  }
                )
  analysis = analyseData(df)
  return = list(names(analysis$inputList))
  }
)

renderUI(
  selectInput("ip_selection",label = "Select Input",
              choices = mydata(),
              selected = mydata()[1]))

renderText(paste0("Reading file: ",input$filepath))
renderText("=====================================")

renderText(input$ip_selection)
```