如何将数据从闪亮的应用程序写入exce / csv文件?恰好我想将股票价格值的值写入excel / csv文件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何将数据从闪亮的应用程序写入exce / csv文件?恰好我想将股票价格值的值写入excel / csv文件相关的知识,希望对你有一定的参考价值。
我想将股票价格的值写入excel / csv文件。但是无法这样做。显示以下错误代码:如果没有活动的响应上下文,则不允许进行操作。 (您试图做只能从反应式表达式或观察器内部完成的操作。)。当我使用反应性数据(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应用程序和后立即执行代码。要允许文件仅在用户准备就绪时才写入,请将“反应性”更改为“观察事件”。添加了“运行”按钮以使其工作。复制并粘贴下面的代码。
顺便说一下,'write.csv'命令中省略了'file =',该命令将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)
以上是关于如何将数据从闪亮的应用程序写入exce / csv文件?恰好我想将股票价格值的值写入excel / csv文件的主要内容,如果未能解决你的问题,请参考以下文章
DT::datatable - 选择要删除的行并写入没有闪亮的 csv