【问题标题】:Data-specific selectInput choices in Rmd ShinyRmd Shiny 中特定于数据的 selectInput 选项
【发布时间】:2015-05-28 23:52:41
【问题描述】:

我正在尝试使用 R-markdown 创建一个小型闪亮应用程序。我的应用看起来像:

---
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 看起来像:

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)
}

myData 看起来像:

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

当我运行它时,我得到以下错误:

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)

我不知道哪里出了问题以及如何解决它。任何帮助表示赞赏!

【问题讨论】:

    标签: r shiny r-markdown


    【解决方案1】:

    您正在尝试根据所选值创建用户界面,但您忘记在此处应用闪亮的基础知识。如果 UI 是基于服务器端值生成的,则必须使用 renderUI 创建它。

    我希望以下内容对您有用.. 我检查了它的结束,正确构建并且页面启动良好。我不知道输入文件和预期值。

    ---
    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)
    ```
    

    【讨论】:

    • 我的 AHA 来了!片刻。效果很棒。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2021-07-12
    • 2020-05-30
    • 2021-05-17
    • 2017-12-02
    • 2019-12-10
    • 2019-01-19
    • 2021-07-09
    • 1970-01-01
    相关资源
    最近更新 更多