R Shiny 可以用于导入文件、运行 R 脚本和导出文件吗?

Eth*_*ker 1 r shiny

谢谢你的时间。

这是一个一般性的小问题,我希望它不会重复。我环顾四周,找不到我的问题的确切答案。

我对 R Shiny 的功能有疑问。我以前从未使用过 R闪亮,并且想知道它是否可以解决我遇到的业务相关问题。

我创建了一个 R 脚本,它接受导入的文件,对其进行操作,然后导出一个新文件。

我的问题是:R Shiny 能否用于构建一个可以在基于 Web 的设置中完成所有这些操作的应用程序?

特别是,我希望其他人运行我的 R 脚本,而不必使用 R。如果 R闪亮有能力做到这一点,这可能会解决我的问题。

谢谢你的时间!

Car*_*bar 5

这是我的第一个答案,但我和 Shiny 一起工作了很多。

当然,你可以按照你的要求去做。您只需合并 fileInput 函数和 downloadButton / downloadLink 即可。

https://shiny.rstudio.com/reference/shiny/1.0.4/downloadButton.html

https://shiny.rstudio.com/gallery/file-upload.html

这里我写了一个简单的例子,你只需要复制并保存为名称 app.R :

library(shiny)

# Define UI for data upload app ----
ui <- fluidPage(

  # App title ----
  titlePanel("Welcome to Ethar data transformation app"),
  # App subtitle
  h4(HTML("Upload your file and click the Download button")),


  # Sidebar layout with input and output definitions ----
  sidebarLayout(

    # Sidebar panel ----
    sidebarPanel(

      # Input: Select a file ----
      fileInput("fileUploaded", "Choose CSV File",
                multiple = FALSE,
                accept = c("text/csv",
                           "text/comma-separated-values,text/plain",
                           ".csv")),

      # Output: Download a file ----
      downloadButton('downloadFile', 'Process the file & Download', class= "action"),

      # CSS style for the download button ----
      tags$style(type='text/css', "#downloadFile { width:100%; margin-top: 35px;}")


    ),

    # Main panel for displaying outputs ----
    mainPanel(

      # Output: Data file ----
      textOutput("done")

    )
  )
)
# Define server logic to read selected file ----
server <- function(input, output) {


  # Download handler in Server
  output$downloadFile <- downloadHandler(
    filename = function() {
      paste('dataProcessed-', Sys.Date(), '.csv', sep='')
    },
    content = function(con) {
      originalData <- input$fileUploaded
      ##############################
      # Transformations in originalData object
      ##############################
      dataProcesed <- originalData
      write.csv(dataProcesed, con)
      output$done <- renderText({
        ifelse(!is.null(dataProcesed),"Transformation done! :D","Error during the process :(")
      })
    }
  )


}

# Create Shiny app ----
shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你!:)