r - 如何将闪亮应用程序中的数据写入 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)
解决方案
在响应式上下文中,它会在运行 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)
推荐阅读
- json - 如何使用角度以特殊字符开头的键打印 JSON 值?
- javascript - 我正在做一个性格测验,我需要一个随机的问题
- c# - C# Trim() 在我的一个项目中不起作用
- django - 如何根据 Django 中传递的路径在 Class-Base-View 中创建逻辑模板和模型
- android - 从与 google play 不同的存储库安装更新
- node.js - Node.js 应用程序在本地工作,但在部署到 Docker 容器时无法工作
- python - 在 Python 中的时间范围内重新采样
- node.js - “错误:9 FAILED_PRECONDITION:找不到匹配的索引。” 查询一处房产
- plotly - 为什么我的绘图图像无法在 dash 应用程序上正确显示
- android - xamarin android中的Webview html音频播放器