首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Shiny和CSV文件

Shiny和CSV文件
EN

Stack Overflow用户
提问于 2019-02-15 22:01:33
回答 1查看 613关注 0票数 0

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

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

这可以实现吗?或者我需要首先将数据集加载到R Studio中吗?

我不确定是不是把自己搞糊涂了,应该只使用read/fileInput函数

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

代码语言:javascript
复制
fileInput("file1", "Choose CSV File",
        accept = c(
          "text/csv")

那么我是不是应该用自己的路径替换"text/csv“呢?

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

提前感谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-20 05:17:16

试试这个,然后给出反馈。

代码语言:javascript
复制
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)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54710953

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档