所以我在R shiny中构建一个应用程序,要求用户上传.csv文件.一旦R shine读入,我不确定如何实际操作该对象使用.一般代码语法如下:
UI文件:
#ui.R
# Define UI for random distribution application
shinyUI(fluidPage(
# Application title
titlePanel("ORR Simulator"),
# Sidebar with controls to select the random distribution type
# and number of observations to generate. Note the use of the
# br() element to introduce extra vertical spacing
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Select the XXX.csv file',
accept=c('text/csv','text/comma-separated-values,text/plain','.csv')),
tags$hr(),
fileInput('file2', 'Select the YYY.csv file',
accept=c('text/csv','text/comma-separated-values,text/plain','.csv')),
tags$hr(),
numericInput("S", "Number of simulations to run:", 100),
mainPanel(
plotOutput("plot")
)
)
))
Run Code Online (Sandbox Code Playgroud)
服务器文件:
#server.R
library(shiny)
shinyServer(function(input, output) {
text1 <- renderText({input$file1})
text2 <- renderText({input$file2})
file1 = read.csv(text1)
file2 = read.csv(text2)
output$plot <- renderPlot({
plot(file1[,1],file2[,2])
})
})
Run Code Online (Sandbox Code Playgroud)
所以我希望text1和text2能够保存包含文件路径的字符串,但似乎并非如此.Ultimatley我只是希望能够读取两个数据集,并从那里能够根据这两个数据集进行输出分析.
当然使用renderText也可能是错误的想法,因此非常感谢任何有关如何做得更好的建议.
这里有一个很好的例子http://shiny.rstudio.com/gallery/file-upload.html.但为了完整起见,我在下面列出了工作答案.关键是你应该使用引用文件file$datapath,并检查输入是否为NULL(当用户尚未上传文件时).
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)
data2 = read.csv(file2$datapath)
output$plot <- renderPlot({
plot(data1[,1],data2[,2])
})
})
})
Run Code Online (Sandbox Code Playgroud)
ui.R
library(shiny)
#ui.R
# Define UI for random distribution application
shinyUI(fluidPage(
# Application title
titlePanel("ORR Simulator"),
# Sidebar with controls to select the random distribution type
# and number of observations to generate. Note the use of the
# br() element to introduce extra vertical spacing
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Select the XXX.csv file',
accept=c('text/csv','text/comma-separated-values,text/plain','.csv')),
tags$hr(),
fileInput('file2', 'Select the YYY.csv file',
accept=c('text/csv','text/comma-separated-values,text/plain','.csv')),
tags$hr(),
numericInput("S", "Number of simulations to run:", 100)
),
mainPanel(
plotOutput("plot")
)
))
)
Run Code Online (Sandbox Code Playgroud)