【问题标题】:Read files from local drive and run r code using shiny从本地驱动器读取文件并使用闪亮运行 r 代码
【发布时间】:2017-10-24 20:09:47
【问题描述】:

我是 Shiny 的新手!这就是我的目标:

我需要使用闪亮运行我的 R 代码并显示输出。 我知道我可以使用下面的代码来运行 r 代码

source("RCODE.R")

但是我的 Rcode 需要从用户本地驱动器通过闪亮的两个文件。

有没有办法让用户选择文件,然后将它们输入到我的 R 代码中,然后在运行代码后显示输出。

我已经关注了

Interactive directory input in shinny App 用于浏览用户文件

感谢您的帮助

【问题讨论】:

  • 你的代码是由server和ui两部分组成的吗?
  • 是的,它由 UI 和服务器组成

标签: r shiny


【解决方案1】:

以下代码有效:

UI.R

library(shiny)

#ui.R
shinyUI(fluidPage(

  # Application title
  titlePanel("Text Mining on R"),

  sidebarLayout(
    sidebarPanel(
      fileInput('file1', 'Select the Input File',
                accept=c('text/csv','text/comma-separated-values,text/plain','.csv')),
      tags$hr(),
      fileInput('file2', 'Select the UserTopic file',
                accept=c('text/csv','text/comma-separated-values,text/plain','.csv'))

    ),
    mainPanel(
      dataTableOutput('table'),
      downloadButton('OutputFile', 'Download')
    )
  ))
)

Server.R

#server.R
library(shiny)
shinyServer(function(input, output) {

  observe({
    file1 = input$file1
    file2 = input$file2
    if (is.null(file1) || is.null(file2)) {
      return(NULL)
    }
    data1 = read.csv(file1$datapath,header = TRUE, sep=",",skipNul = TRUE)
    data2 = read.csv(file2$datapath,header = TRUE, sep=",",skipNul = TRUE)
   source("RCode.R", local = TRUE)
    #output$table <- renderDataTable(output2)
    output$table <- renderDataTable({
      my_function(file1$datapath,file2$datapath)
    })

    output$OutputFile <- downloadHandler(


      filename = function() {
        paste("OutputFile", " ",Sys.Date(),".csv",sep="")
       },

      content = function(file) {


        write.csv(my_function(file1$datapath,file2$datapath), file, sep = ",",
                    row.names = FALSE)
      }
    )
  })

})

此代码从 Shiny 界面读取两个 csv 文件并显示 R 代码的输出,它还有一个下载显示输出的链接。

【讨论】:

    猜你喜欢
    • 2017-01-15
    • 2019-04-04
    • 2015-12-24
    • 2019-11-12
    • 2018-05-30
    • 1970-01-01
    • 1970-01-01
    • 2016-11-05
    • 1970-01-01
    相关资源
    最近更新 更多