Why does my Shiny app import, analyze, and display data, but not export or plot it?

huangapple go评论73阅读模式
英文:

Why does my Shiny app import, analyze, and display data, but not export or plot it?

问题

我已经编写了一个Shiny应用程序,用于从CSV文件导入数据,进行分析,显示在表格中,然后根据请求绘制和导出数据。

问题是,当要求导出或绘制结果时,应用程序会显示错误消息" LREdata不存在"。

应用程序:

ui <- fluidPage(
  titlePanel("LRE Analysis"),
  sidebarLayout(
    sidebarPanel(
      fileInput("file", "Select a file"),
      numericInput("N", "BaselineF:", 1000),
      numericInput("F", "Lowest F ratio:", 1.8)
    ),
    mainPanel(
      tabsetPanel(type = "tabs",
                  tabPanel("Data", DT::dataTableOutput("table2"),
                           downloadButton("downloadData", "Download")),
                  tabPanel("Plot", plotOutput("plotx"))
      )
    )
  )
)

server <- function(input, output) {

  input_file <- reactive({
    if (is.null(input$file)) {
      return("")
    }

    read.csv(file = input$file$datapath)
  })

  output$table2 <- DT::renderDataTable({
    # 在这里执行分析,结果存储在名为LREdata的数据框中
    print(LREdata)
  })

  output$downloadData <- downloadHandler(
    filename = function() {
      paste(input$file, ".csv", sep = "")
    },
    content = function(file) {
      write.csv(LREdata, file, row.names = FALSE)
    }
  )

  output$plotx <- renderPlot({
    ggplot(LREdata, aes(x=factor(TimePeriod),y=PercentageMatches, colour = QualityMatch)) + geom_col()+ facet_grid(vars(TargetSubset), vars(Oligo))                                                     
  })

}

shinyApp(ui = ui, server = server)

请注意,我只翻译了代码部分,没有包括问题中的额外内容。

英文:

I've written a shiny app to import data from a csv file, analyse it, display in a table, and then plot and export upon request.

The problem is that the app will import/analyse/display results (a df called LREdata), but when it is requested to either export or plot the results, it shows error "the LREdata doesn't exist".

The app:

ui &lt;- fluidPage(
  titlePanel(&quot;LRE Analysis&quot;),
  sidebarLayout(
    sidebarPanel(
      fileInput(&quot;file&quot;, &quot;Select a file&quot;),
	numericInput(&quot;N&quot;, &quot;BaselineF:&quot;, 1000),
	numericInput(&quot;F&quot;, &quot;Lowest F ratio:&quot;, 1.8)
    ),
    mainPanel(
      tabsetPanel(type = &quot;tabs&quot;,
                  tabPanel(&quot;Data&quot;, DT::dataTableOutput(&quot;table2&quot;),
		downloadButton(&quot;downloadData&quot;, &quot;Download&quot;)),
tabPanel(&quot;Plot&quot;, plotOutput(&quot;plotx&quot;))
                  
      )
       )
)
)


server &lt;- function(input, output) {

  input_file &lt;- reactive({
    if (is.null(input$file)) {
      return(&quot;&quot;)
    }

    read.csv(file = input$file$datapath)
  })

  output$table2 &lt;- DT::renderDataTable({
#Analysis performs here, results in df called LREdata
   
print(LREdata)
  })

output$downloadData &lt;- downloadHandler(
    filename = function() {
      paste(input$file, &quot;.csv&quot;, sep = &quot;&quot;)
    },
    content = function(file) {
      write.csv(LREdata, file, row.names = FALSE)
    }
  )

output$plotx &lt;- renderPlot({
ggplot(LREdata, aes(x=factor(TimePeriod),y=PercentageMatches, colour = QualityMatch)) + geom_col()+ facet_grid(vars(TargetSubset), vars(Oligo))                                                     
})

}

shinyApp(ui = ui, server = server)

答案1

得分: 0

在Shiny中,每个响应式元素都有自己的环境,因此当你在renderDataTable()中生成名为LREdata的数据框时,该对象无法被服务器端的任何其他函数访问。在这种情况下,如果你希望在多个服务器函数中使用一个对象,你必须将其分配给一个响应式元素。在这里:

### 在input_file之后添加这个新的响应式元素,并将所有分析移动到这里
LREdata <- reactive({
  # 在此执行分析,结果存储在名为LREdata的数据框中
  LREdata
})

output$table2 <- DT::renderDataTable(LREdata())

output$downloadData <- downloadHandler(
    filename = function() {
      paste(input$file, ".csv", sep = "")
    },
    content = function(file) {
      write.csv(LREdata(), file, row.names = FALSE)
    }
  )

现在,LREdata被全局定义,所有后续的代码都可以读取它。

英文:

In Shiny each reactive element has its own environment, so when you generate a dataframe named LREdata in your renderDataTable() the object is not accessible to any other functions in your server side. In such cases that you want to use an object in multiple server functions you have to assign it to a reactive element. Here:

### Add this new reactive element after your input_file and move all your analysis from the renderDataTable here
  LREdata &lt;- reactive({

  #Analysis performs here, results in df called LREdata
  LREdata
  })

  output$table2 &lt;- DT::renderDataTable(LREdata())

output$downloadData &lt;- downloadHandler(
    filename = function() {
      paste(input$file, &quot;.csv&quot;, sep = &quot;&quot;)
    },
    content = function(file) {
      write.csv(LREdata(), file, row.names = FALSE)
    }
  )

Now LREdata is defined globally and all other subsequent code can read it.

huangapple
  • 本文由 发表于 2023年6月1日 12:53:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76378759.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定