Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 无法使我的反应变量工作_R_Shiny - Fatal编程技术网

R 无法使我的反应变量工作

R 无法使我的反应变量工作,r,shiny,R,Shiny,由于我对R脚本相当陌生,我很难让我的应用程序正常工作 我想要达到的目标是: 用户上传CSV文件 用户检查文件预览(安全检查) 用户执行查询 查询显示在主面板上 结果可在csv上下载 我能够执行所有这些点,只要我上传一个文件。它们同时发生,扼杀了应用程序的目的 问题:我有三个输出,需要在特定操作中执行(加载预览、运行查询、下载查询结果) 有人能帮我吗 这是我的密码: #Libraries to load library(shiny) library(dplyr) library('devtool

由于我对R脚本相当陌生,我很难让我的应用程序正常工作

我想要达到的目标是:

  • 用户上传CSV文件
  • 用户检查文件预览(安全检查)
  • 用户执行查询
  • 查询显示在主面板上
  • 结果可在csv上下载
  • 我能够执行所有这些点,只要我上传一个文件。它们同时发生,扼杀了应用程序的目的

    问题:我有三个输出,需要在特定操作中执行(加载预览、运行查询、下载查询结果)

    有人能帮我吗

    这是我的密码:

    #Libraries to load
    
    library(shiny)
    library(dplyr)
    library('devtools')
    
    #--------------------------------------
    # Interface
    #--------------------------------------
    
    ui <- fluidPage(
    
      # Application title
      titlePanel("Content Upload Report"),
    
      sidebarPanel(
    
        h4("1. Select the desired data range and upload your file. A preview will be shown once it gets loaded."),
    
        # Sidebar with a data range input
        dateRangeInput("dates", "Date range",
                       start = "2017-09-01", end = "2017-09-30", 
                       min = "2017-01-01", max = "2018-12-31",
                       format = "yyyy-mm-dd", startview = "month", weekstart = 1,
                       language = "en", separator = " to "),
    
        #Sidebar with a file input
        fileInput("file1", "Choose CSV File",
                  accept = c(
                    "text/csv",
                    "text/comma-separated-values,text/plain",
                    ".csv")),
    
        h4("2. Once you see that the list seems correct, click on the button below."),
    
        tableOutput("preview"),
    
        #Submit button
        actionButton("startQuery","Start Query",icon ("search"), width = NULL, class="butt"),
        tags$head(tags$style(".butt{background-color:#007fad;} .butt{color: white;}")),
    
        tags$br(),
    
        #Warning about loading time
        helpText("When you click the button above, it might take a while until the results are ready.",
                 "The size of your list directly impacts the waiting time for the query."),
    
        #Horizontal Line
        tags$hr(),
    
        #Download Results
        downloadButton('downloadData1', label = "Download Results", class = "btmcolor"),
        tags$head(tags$style(".btmcolor{background-color:#007fad;} .btmcolor{color: white;}"))
    
        ),
    
      mainPanel(
    
        dataTableOutput("result"),
        tags$style(type="text/css", '#result tfoot {display:none;}')
      )
    
    )
    
    #--------------------------------------
    # Server
    #--------------------------------------
    
    server <- function(input, output) {
    
      d<-reactiveValues()
    
       output$preview <- renderTable({
    
        # input$file1 will be NULL initially. After the user selects
        # and uploads a file, it will be a data frame with 'name',
        # 'size', 'type', and 'datapath' columns. The 'datapath'
        # column will contain the local filenames where the data can
        # be found.
    
        inFile <- input$file1
    
        if (is.null(inFile))
          return(NULL)
    
        df <- read.csv(inFile$datapath, header = FALSE, sep =",")
    
        #This will be printed on the preview
        head(df)
    
      })
    
      output$result <- renderDataTable({
    
        # Fix data input to format yyyymmdd
        tmp_str <- paste(as.character(input$dates), collapse = " and ")
        tmp_str <- gsub("-", "", tmp_str)
    
        # input$file1 will be NULL initially. After the user selects
        # and uploads a file, it will be a data frame with 'name',
        # 'size', 'type', and 'datapath' columns. The 'datapath'
        # column will contain the local filenames where the data can
        # be found.
    
        inFile <- input$file1
    
        if (is.null(inFile))
          return(NULL)
    
        df <- read.csv(inFile$datapath, header = FALSE, sep =",")
    
        #Prepare file for query
    
        #read the user file as a single string into csvString
        csvString <- paste(readLines(inFile$datapath), collapse=", ")
        print(csvString)
    
        #put all emails into 1 string with quotes around each
        csvString <- paste0(sprintf("%s", csvString), collapse = ", ")
    
        #Authenticate on DB
        ds <- "authentication string. custom library"
    
        #Run Query 
        query <- paste0("
                        SELECT item_id, country, total_new_images
                        FROM inventory
                        WHERE item_id IN (", csvString, ")
                        GROUP BY item_id, country
                        ORDER BY item_id
                        ")
    
    
        d$data <- ds$execute_query(query) #custom function
        d$result1 <- as.data.frame(d$data)
    
      })
    
      #------------------------------------------
      # Download Output
      #------------------------------------------
    
      output$downloadData1 <- downloadHandler(
        filename = function() {
              tmp<- paste(as.character(input$dates), collapse = "_")
              tmp <- gsub("-", "", tmp)
          paste0("content_upload_",tmp,".csv") },
        content = function(file) {
          write.csv(d$result1, file)
        })
    }
    
    shinyApp(ui = ui, server = server)
    
    #要加载的库
    图书馆(闪亮)
    图书馆(dplyr)
    库(“devtools”)
    #--------------------------------------
    #接口
    #--------------------------------------
    
    ui将
    输出
    函数封装在
    observeEvent
    中将有助于做到这一点

    #Libraries to load
    
    library(shiny)
    library(dplyr)
    library('devtools')
    
    #--------------------------------------
    # Interface
    #--------------------------------------
    
    ui <- fluidPage(
    
      # Application title
      titlePanel("Content Upload Report"),
    
      sidebarPanel(
    
        h4("1. Select the desired data range and upload your file. A preview will be shown once it gets loaded."),
    
        # Sidebar with a data range input
        dateRangeInput("dates", "Date range",
                       start = "2017-09-01", end = "2017-09-30", 
                       min = "2017-01-01", max = "2018-12-31",
                       format = "yyyy-mm-dd", startview = "month", weekstart = 1,
                       language = "en", separator = " to "),
    
        #Sidebar with a file input
        fileInput("file1", "Choose CSV File",
                  accept = c(
                    "text/csv",
                    "text/comma-separated-values,text/plain",
                    ".csv")),
    
        h4("2. Once you see that the list seems correct, click on the button below."),
    
        tableOutput("preview"),
    
        #Submit button
        actionButton("startQuery","Start Query",icon ("search"), width = NULL, class="butt"),
        tags$head(tags$style(".butt{background-color:#007fad;} .butt{color: white;}")),
    
        tags$br(),
    
        #Warning about loading time
        helpText("When you click the button above, it might take a while until the results are ready.",
                 "The size of your list directly impacts the waiting time for the query."),
    
        #Horizontal Line
        tags$hr(),
    
        #Download Results
        downloadButton('downloadData1', label = "Download Results", class = "btmcolor"),
        tags$head(tags$style(".btmcolor{background-color:#007fad;} .btmcolor{color: white;}"))
    
      ),
    
      mainPanel(
    
        dataTableOutput("result"),
        tags$style(type="text/css", '#result tfoot {display:none;}')
      )
    
    )
    
    #--------------------------------------
    # Server
    #--------------------------------------
    
    server <- function(input, output) {
    
      d<-reactiveValues()
    
      output$preview <- renderTable({
    
        # input$file1 will be NULL initially. After the user selects
        # and uploads a file, it will be a data frame with 'name',
        # 'size', 'type', and 'datapath' columns. The 'datapath'
        # column will contain the local filenames where the data can
        # be found.
    
        inFile <- input$file1
    
        if (is.null(inFile))
          return(NULL)
    
        df <- read.csv(inFile$datapath, header = FALSE, sep =",")
    
        #This will be printed on the preview
        head(df)
    
      })
    
      observeEvent(input$startQuery,{
    
    
        output$result <- renderDataTable({
    
          # Fix data input to format yyyymmdd
          tmp_str <- paste(as.character(input$dates), collapse = " and ")
          tmp_str <- gsub("-", "", tmp_str)
    
          # input$file1 will be NULL initially. After the user selects
          # and uploads a file, it will be a data frame with 'name',
          # 'size', 'type', and 'datapath' columns. The 'datapath'
          # column will contain the local filenames where the data can
          # be found.
    
          inFile <- input$file1
    
          if (is.null(inFile))
            return(NULL)
    
          df <- read.csv(inFile$datapath, header = FALSE, sep =",")
    
          #Prepare file for query
    
          #read the user file as a single string into csvString
          csvString <- paste(readLines(inFile$datapath), collapse=", ")
          print(csvString)
    
          #put all emails into 1 string with quotes around each
          csvString <- paste0(sprintf("%s", csvString), collapse = ", ")
    
          #Authenticate on DB
          ds <- "authentication string. custom library"
    
          #Run Query 
          query <- paste0("
                          SELECT item_id, country, total_new_images
                          FROM inventory
                          WHERE item_id IN (", csvString, ")
                          GROUP BY item_id, country
                          ORDER BY item_id
                          ")
    
    
          d$data <- ds$execute_query(query) #custom function
          d$result1 <- as.data.frame(d$data)
    
        })
      })
    
    
      #------------------------------------------
      # Download Output
      #------------------------------------------
    
      observeEvent(input$downloadData1,{
    
        output$downloadData1 <- downloadHandler(
          filename = function() {
            tmp<- paste(as.character(input$dates), collapse = "_")
            tmp <- gsub("-", "", tmp)
            paste0("content_upload_",tmp,".csv") },
          content = function(file) {
            write.csv(d$result1, file)
          })
    
      })
    
    }
    
    shinyApp(ui = ui, server = server)
    
    #要加载的库
    图书馆(闪亮)
    图书馆(dplyr)
    库(“devtools”)
    #--------------------------------------
    #接口
    #--------------------------------------
    
    非常感谢!你这方面的建议真是太好了。现在我只需要以某种方式获取下载结果。它以前工作过,但现在似乎不能正常工作。对不起,现在有什么问题?没有下载任何数据?有什么测试吗?