首页 > 解决方案 > 如何将闪亮应用程序中的数据写入 excel/csv 文件?正是我想将股票价格的值写入 excel/csv 文件

问题描述

我想将股票价格值写入 excel/csv 文件,但我无法这样做。显示以下错误代码:Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)当我使用反应数据(dataInput)时,错误消息显示为“无法强制类'c(“reactiveExpr”,“reactive”)'到data.frame

代码附在此处:

加载包----

library(shiny)
library(quantmod)
#edited the code.this can be run directly

# User interface ----
ui <- fluidPage(
  titlePanel("stockVis"),

  sidebarLayout(
    sidebarPanel(
      helpText("Select a stock to examine.

        Information will be collected from Yahoo finance."),
      textInput("symb", "Symbol", "SPY"),

      dateRangeInput("dates",
                     "Date range",
                     start = "2013-01-01",
                     end = as.character(Sys.Date())),

      br(),
      br(),

      checkboxInput("log", "Plot y axis on log scale",
                    value = FALSE)

      #checkboxInput("adjust",
      #"Adjust prices for inflation", value = FALSE)
    ),

    mainPanel(plotOutput("plot"), tableOutput("view")))



)


# Server logic
server <- function(input, output) {

  dataInput <- reactive({
    getSymbols(input$symb, src = "yahoo",
               from = input$dates[1],
               to = input$dates[2],
               auto.assign = FALSE)


  }) Blockquote
  output$plot <- renderPlot({

    chartSeries(dataInput(), theme = chartTheme("white"),
                type = "line", log.scale = input$log, TA = NULL)
  })

  output$view <- renderTable({(dataInput() )
  }, include.rownames = TRUE)
  #trying to export the data
  write.csv(dataInput(),row.names = TRUE)

}`enter code here`

# Run the app
shinyApp(ui, server)

标签: rshiny

解决方案


在响应式上下文中,它会在运行 Shiny 应用程序以及股票代码开始变化时立即尝试执行代码。要允许文件仅在用户准备好时写入,请将“reactive”更改为“observe event”。添加了一个“运行”按钮以使其工作。复制并粘贴下面的代码。

顺便说一句,因为在“write.csv”命令中省略了“file=”,这会将 csv 文件滚动到控制台。

这是一个很好的实用程序,可以轻松地将股票价格下载到 csv 文件。

library(shiny)
library(quantmod)
#edited the code.this can be run directly

# User interface ----
ui <- fluidPage(
  titlePanel("stockVis"),

  sidebarLayout(
    sidebarPanel(
      helpText("Select a stock to examine.

        Information will be collected from Yahoo finance."),
      textInput("symb", "Symbol", "SPY"),

      dateRangeInput("dates",
                     "Date range",
                     start = "2013-01-01",
                     end = as.character(Sys.Date())),

      br(),
      br(),

      checkboxInput("log", "Plot y axis on log scale",
                    value = FALSE),

      #checkboxInput("adjust",
      #"Adjust prices for inflation", value = FALSE),
      actionButton(inputId = "run",label="Run"),
    ),

    mainPanel(plotOutput("plot"), tableOutput("view")))

)

# Server logic
server <- function(input, output) {

  dataInput <- function() {
    getSymbols(input$symb, src = "yahoo",
               from = input$dates[1],
               to = input$dates[2],
               auto.assign = FALSE)
  }

  observeEvent (input$run, {

   output$plot <- renderPlot({

    chartSeries(dataInput(), theme = chartTheme("white"),
                type = "line", log.scale = input$log, TA = NULL)
    })

    output$view <- renderTable({(dataInput() )
    }, include.rownames = TRUE)
    #trying to export the data
    write.csv(dataInput(),row.names = TRUE)

  })
}
# Run the app

shinyApp(ui, server)

推荐阅读