因此,我正在R Shiny中构建一个需要用户上传.csv文件的应用程序。一旦由R Shiny读入,我不确定如何实际操纵该对象以使用。常规代码语法如下:
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")
)
)
))
服务器文件:
#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])
})
})
因此,我希望text1和text2包含包含文件所在路径的字符串,但事实并非如此。Ultimatley我只希望能够读取两个数据集,并从那里能够基于这两个数据集进行分析并输出。
当然,使用renderText可能也是一个错误的主意,因此,关于如何更好地实现此目的的任何建议都将受到赞赏。
http://shiny.rstudio.com/gallery/file-upload.html就是一个很好的例子。但是为了完整起见,我在下面提供了工作答案。关键是您应该使用来引用文件file$datapath
,并检查输入是否为NULL(当用户尚未上传文件时)。
服务器
#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])
})
})
})
用户界面
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")
)
))
)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句