首页 > 解决方案 > 将 textAreaInput 中的文本创建发送到 Google Cloud Storage

问题描述

在使用googleCloudStorageR包使用 *json 文件进行身份验证后,我尝试在 a 中写入文本textAreaInput并作为 *txt 发送到我的 Google Cloud Storage 存储桶,但没有成功。输出总是:

Listening on http://127.0.0.1:3842
Auto-refreshing stale OAuth token.
Warning: Error in : Path 'NA' does not exist
  [No stack trace available]

我没有证书的例子:

# Packages
require(rgdal)
require(shiny)
require(shinythemes)
require(googleCloudStorageR)


# Create the shiny dash
ui <- fluidPage(
  theme = shinytheme("cosmo"),
  titlePanel(title="My Map Dashboard"),  
  sidebarLayout(
    sidebarPanel(
      textAreaInput("text_input","Selected data"),
      actionButton("sendMSG", "Send to server")
    ),
    mainPanel(
      textOutput("sendMSG")
    )
  )
)
server <- function(input, output){

  MyMSG <- reactive({
    
    output$text_output <- renderText({input$text_input})
    
  })
  
  # Read text write in the box and send to the server
  
  observeEvent(input$sendMSG, {
    
    filename = function() {
      paste0("MyText",Sys.Date(),".txt",sep="")
    }
    content = function(file) {
      write.table(MyMSG(), file, row.names = F)
    }
    # Send output to Google Cloud Storage
    gcs_get_bucket("forestcloud")
    # *txt files
    all_txt_est <- list.files(pattern="\\.txt$", full.names=TRUE)
    for(f in 1:length(all_txt_est)){
      all_txt_est_name<-all_txt_est[f] 
      gcs_upload(all_txt_est_name, name=all_txt_est_name)
    }
    ###  
 
})
}
shinyApp(ui, server)
##

请帮忙解决一下?

标签: rshinyshinydashboardshiny-reactivity

解决方案


大概all_txt_est是空的。您不在write.table观察者中执行该函数,您只需定义一个执行的函数write.table,但您不使用此函数。

不要在导体output内部定义槽reactive,这没有任何意义。

也许以下是您想要的。我不了解谷歌云存储,所以我不确定关于这一点的代码是否正确。

server <- function(input, output){
    
  output$sendMSG <- renderText({input$text_input}) # you can do that but this will render the same as the textAreaInput
      
  observeEvent(input$sendMSG, {

    filePath <- tempfile(fileext = ".txt")
    writeLines(input$text_input, filePath)
    # Send output to Google Cloud Storage
    gcs_get_bucket("forestcloud")
    gcs_upload(filePath, name = filePath)
 
  })

}

推荐阅读