【问题标题】:How to load a file in Shiny only once?如何仅在 Shiny 中加载文件一次?
【发布时间】:2017-10-13 22:29:36
【问题描述】:

我只是想知道如何只在 server.R 中运行 read.csv 一次?在下面的代码中,每次我更改 ui.R 中的输入时,代码运行 read.csv 需要几秒钟,因此我期待在开始时只加载一次文件然后使用相同的ui.R 中输入时的数据发生变化。谢谢 这是我的代码:

ui.R

shinyUI(fluidPage(
     headerPanel("myApp"),
     fluidRow(   sidebarPanel(
    selectInput("myOptions", "myOptions:",
                list("1" = "1", 
                     "2" = "2",
                     "3" = "3"))
     )),
     hr(),
     plotlyOutput("prescription"),   
     hr()   
      ))

服务器.R

library(utils)
library(plotly)
library(plyr)


shinyServer(function(input, output) {


  diseases<-eventReactive({
    diseases=read.csv("diseases_94.csv",header=F)
 })


  output$prescription <- renderPlotly( {

   myDiseases=diseases
   freq=count(myDiseases,"DISEASES")

   p<-plot_ly(freq, labels = ~DISEASES, values = ~freq, type = 'pie',
        textposition = 'inside',
        textinfo = 'label+percent',
        insidetextfont = list(color = '#FFFFFF'),
        hoverinfo = 'text',
        text = ~paste(DISEASES),
        marker = list(line = list(color = '#FFFFFF', width = 1)))



 p


 })


 })

【问题讨论】:

  • 如果你只想加载一次,为什么只能在服务器函数之外加载呢?也许您可以告诉我们更多您希望何时以及如何读取数据,...
  • @BigDataScientist 感谢您的回复。当我运行闪亮的应用程序时,我只需要在开始时加载。然后我应该将它作为输入参数传递给 shinyServer 函数吗?
  • 你可以像普通变量一样使用它,..
  • @BigDataScientist 明白了!它现在正在工作。非常感谢

标签: r shiny


【解决方案1】:

使用shinyServer 之外的变量。这应该只在开头加载文件。

【讨论】:

    猜你喜欢
    • 2022-11-20
    • 2021-06-27
    • 2020-07-09
    • 1970-01-01
    • 1970-01-01
    • 2021-06-07
    • 1970-01-01
    • 2020-12-07
    • 1970-01-01
    相关资源
    最近更新 更多