简体   繁体   中英

How to write data from a shiny app to an excel/csv file? Precisely I want to write the values of stock price to an excel/csv file

I want to write the stock price values to an excel/csv file but I am unable to do so. The following error code is displayed: 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.) 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.) Where when I use the reactive data(dataInput ) then the error message reads as "cannot coerce class 'c("reactiveExpr", "reactive")' to a data.frame

Code is enclosed here:

Load packages ----

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)

In the reactive context, it's trying to execute the code immediately upon running the Shiny app and as soon as the stock symbol starts changing. To permit the file to write only when the user is ready, change 'reactive' to 'observe event'. A 'run' button has been added to make it work. Copy and paste the code below.

By the way, because the 'file=' is omitted in the 'write.csv' command, which scrolls the csv file to the console.

This is a nice utility which makes it easy to download stock prices to a csv file.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM