【问题标题】:Shiny and CSV files闪亮和 CSV 文件
【发布时间】:2019-07-09 16:37:48
【问题描述】:

我对编程非常陌生,并且一直在观看有关如何通过 R Studio 构建闪亮应用程序的视频。

我正在努力寻找的一件事是我可以使用的简单代码,它要求 Shiny 选择一个位于我桌面上的 CSV 文件。

这可以实现还是我需要先将数据集加载到 R Studio 中?

我不确定我是否让自己感到困惑,是否应该改用 read/fileInput 函数

所有帮助指南都显示此代码:

fileInput("file1", "Choose CSV File",
        accept = c(
          "text/csv")

所以我只是用自己的路径替换“text/csv”吗?

我对相互矛盾的信息感到非常困惑,希望有人可以用外行的话为我分解一下

提前致谢

【问题讨论】:

  • 这应该会有所帮助:shiny.rstudio.com/gallery/file-upload.html
  • 设法让它工作......非常感谢您的帮助!我只是上传文件,我应该能够通过 R Studio 访问它吗?
  • 很高兴听到,干得好!在 Shinyapp 中你应该可以使用它,是的。

标签: r csv shiny


【解决方案1】:

试试这个并反馈。

library(shiny)
library(ggplot2)
#ui.R
ui <- fluidPage(
  titlePanel("My shiny app"), sidebarLayout(
sidebarPanel(
  helpText("This app shows how a user can upload a csv file. Then, plot the data.
          Any file can be uploaded but analysis is only available
          if the data is in same format as the sample file, downloadable below
          "),
  a("Data to be plotted", href="https://www.dropbox.com/s/t3q2eayogbe0bgl/shiny_data.csv?dl=0"),
  tags$hr(),
  fileInput("file","Upload the file"), 
  h5(helpText("Select the read.table parameters below")),
  checkboxInput(inputId = 'header', label = 'Header', value = TRUE),
  checkboxInput(inputId = "stringAsFactors", "stringAsFactors", FALSE),
  br(),
  radioButtons(inputId = 'sep', label = 'Separator', choices = c(Comma=',',Semicolon=';',Tab='\t', Space=''), selected = ',')
),
mainPanel(
  uiOutput("tb"),
  plotOutput("line")             
)
)
)

#server.R
server <- function(input,output){
data <- reactive({


file1 <- input$file
if(is.null(file1)){return()} 

read.table(file=file1$datapath, sep=input$sep, header = input$header, stringsAsFactors = input$stringAsFactors)})

output$filedf <- renderTable({
if(is.null(data())){return ()}
input$file
}) 

output$sum <- renderTable({
if(is.null(data())){return ()}
summary(data())
})

output$table <- renderTable({
if(is.null(data())){return ()}
data()
})

output$line <- renderPlot({
if (is.null(data())) { return() }
print(ggplot(data(), aes(x=date, y=aa)) + geom_line()+ facet_wrap(~station)) })

output$tb <- renderUI({if(is.null(data()))
h5()               
else
  tabsetPanel(tabPanel("About file", tableOutput("filedf")),tabPanel("Data", tableOutput("table")),tabPanel("Summary", tableOutput("sum")))
})
}


shinyApp(ui = ui, server = server)

【讨论】:

    猜你喜欢
    • 2017-07-26
    • 2015-10-14
    • 1970-01-01
    • 2015-10-13
    • 2016-07-25
    • 2023-03-19
    • 2017-11-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多