如何从R Shiny更新JSON文件?

时间:2019-10-23 20:52:26

标签: r shiny

我有此代码:

# Preloads ---- 

library(shiny)
library(dplyr)
library(xlsx)
library(jsonlite)
library(httr)
library(svDialogs)
library(downloader)
params <- readLines("params.json")


# end preloads ---- 


# Define server logic 
shinyServer(function(input, output) {
    my_params <- reactive({ 
        my_params[4] <- paste0("          \"time_period\":[{\"start_date\":\"", 
                               Sys.Date()-10*365, "\",\"end_date\":\"", 
                               Sys.Date(), "\"}],")
        my_params[5] <- paste0("          \"recipient_search_text\": [\"",
                               as.character(input$the_duns), "\"]")
    })    


    observeEvent(input$get_api, { 

        writeLines(my_params,"params.json")

    })  

    the_data <- reactive({ 
        # the excel workbook to download 
        pages<-list()
        url<-"https://api.usaspending.gov/api/v2/download/awards"
        API_response <- httr::POST(url, body = upload_file("params.json"))
        stop_for_status(API_response)
        json <- content(API_response, "text",encoding = "UTF-8")
        API_data <- fromJSON(json)

        download(API_data[["url"]], dest="dataset.zip", mode="wb") 
        unzip("dataset.zip", exdir = getwd())
        output <- read.table(unz("dataset.zip", "all_contracts_prime_awards_1.csv"),
                             header=T, quote="\"", sep="," ,fill = TRUE)
        output1 <- read.table(unz("dataset.zip", "all_contracts_prime_awards_1.csv"),
                              header=T, quote="\"", sep="," ,fill = TRUE)

我正在尝试读取一个预先存在的JSON文件,根据用户输入更改它的某些部分,然后再次写入JSON文件。然后,我希望使用该JSON文件来调用API并从zip文件中提取数据。我需要帮助弄清楚为什么我的JSON文件没有使用input $ duns中的数字进行更新。有人有建议吗?

0 个答案:

没有答案