首页 > 解决方案 > 如何在 r shiny 中编辑表格

问题描述


library(quantmod)
library(shiny)

ui <- fluidPage(
    textInput("Stock","Input Stock"),
    textInput("Date","Input Start Date"),
    textInput("Dateto","Input End Date"),
    actionButton("GO","GO"),
    tableOutput("table")
)

server <- function(input, output, session){

    data <- eventReactive(input$GO,{
        req(input$Stock)
        getSymbols(input$Stock,src = "yahoo", from=input$Date,to=input$Dateto,auto.assign=F)

    })


    output$table <- renderTable({
        data()
    })

}
shinyApp(ui, server)

这是我当前 r shiny 文档的代码,它正在按要求输出输入股票的数据表,但是我将如何在输出之前编辑表“数据”,例如在其中添加一个带有移动平均线的列

标签: rshiny

解决方案


如果您不需要将data对象保留为xts zoo,则一种选择是将调用结果存储到getSymbols,转换为data.frame,然后分配一个新列。在下面的示例中,我为您的输入任意选择值(跟踪过去 90 天的 AAPL 股票),并使用以下rollmeanr()函数计算滚动 7 天平均收盘价zoo

raw_data <- getSymbols("AAPL",src = "yahoo", from=Sys.Date()-90,to=Sys.Date(),auto.assign=F)
df <- as.data.frame(raw_data)
# k is the window size for rolling mean
# fill = NA tells the function to fill missing values with NA
df$moving_7_day <- rollmeanr(df$AAPL.Close, k = 7, fill = NA)

推荐阅读