如何绘制使用闪亮上传的数据集? [英] How to plot uploaded dataset using shiny?
问题描述
我是R-shiny应用程序的新手,我的应用程序非常简单。它有两个选项卡,第一,我上传一个文件,如csv,然后在第二个选项卡,我选择将被绘制的列,我的解决方案分散在几个例子每个不同于我的,我想上传的数据集可以看到,并且可以在所有功能中使用,而不仅仅是上传它。
我的服务器.R
库(shiny)
shinyServer(函数(输入,输出){
输出$ contents< - renderTable({
inFile< - 输入$ file1
if(is.null(inFile))
return(NULL)
read.csv(inFile $ datapath,header = input $ header,sep = input $ sep,
quote =输入$ quote)
))
输出$ MyPlot< - renderPlot({
x< - contents()$ contents [,c (输入$ xcol,输入$ ycol)]
bins< - nrow(contents())
hist(x,breaks = bins,col ='darkgray',border ='white')
$)
))
ui.R
库(闪亮)
库(数据集)
shinyU I(fluidPage(
titlePanel(Column Plot),
tabsetPanel(
tabPanel(上传文件,
titlePanel(上传文件),
sidebarLayout (
sidebarPanel(
fileInput('file1','选择CSV文件',
accept = c('text / csv',$ b $'text / comma-separated-values, text / plain',
'.csv'))
),
mainPanel(
tableOutput('contents')
)
)
),
tabPanel(First Type,
pageWithSidebar(
headerPanel('My First Plot'),
sidebarPanel(
selectInput(' xcol','X Variable',names(content)),
selectInput('ycol','Y Variable',names(content),
selected = names(content)[[2]])
),
mainPanel(
p lotOutput('MyPlot')
)
)
)
)
)
)
我已经试过了,但我刚刚开始这么做,请问我该怎么做?
<您可以创建一个被动数据集(例如data
),其中您的应用程序将读取上传的文件并更新输入 - 在这种情况下,数据框的名称并将其传递给render *
函数。
库(闪亮)
库(数据集)
ui< - shinyUI(fluidPage(
titlePanel(Column Plot),
tabsetPanel(
tabPanel(上传文件,
titlePanel(上传文件),
sidebarLayout(
sidebarPanel(
fileInput('file1','选择CSV文件',
accept = c('text / csv',
' text / comma-separated-values,text / plain',
'.csv')),
#增加了从
#http:// shiny上传数据的界面。 rstudio.com/gallery/file-upload.html
tags $ br(),
checkboxInput('header','Header',TRUE),
radioButtons('sep','Separator ',
c(逗号=',',
Semicolon =';',
Tab ='\t'),
','),
radioButtons('quote','Quote',
c(None ='' ',
'双引号'='''',
'单引号'='),
''')
),
mainPanel(
tableOutput('contents')
)
)
),
tabPanel(First Type,
pageWithSidebar(
) headerPanel('我的第一个情节'),
sidebarPanel(
#空输入 - 它们会在数据上传后更新
selectInput('xcol','X变量',),
selectInput('ycol','Y Variable',,selected =)
),
mainPanel(
plotOutput('MyPlot')
)
)
)
)
)
)
服务器< - shinyServer(函数(输入,输出,会话){
#增加了会话,因为updateSelectInput需要它
data < - reactive({
req(input $ file1)##?req#要求输入可用
inFile< - input $ file1
#使用以下数据集进行测试:write.csv(mtcars,mtcars.csv)
#和write.csv(iris,iris.csv)
df< - read.csv(inFile $ datapath,header =输入$ header,sep =输入$ sep,$ b $ quote =输入$ quote)
#更新输入(你可以创建一个观察者与updateSel ...)
#你也可以限制你的选择。如果您只想选择数字
#变量,您可以设置choices = sapply(df,is.numeric)
#这取决于您稍后想要做什么。
$ b updateSelectInput(session,inputId ='xcol',label ='X Variable',
choices = names(df),selected = names(df))
updateSelectInput(session ,inputId ='ycol',label ='Y Variable',
choices = names(df),selected = names(df)[2])
return(df)
}}
输出$ contents< - renderTable({
data()
})
输出$ MyPlot< - renderPlot({
#为直方图:删除第二个变量(它也必须是数字):
#x < - data()[,c(input $ xcol,input $ ycol)]
#bins < - nrow(data())
#hist(x,breaks = bins,col ='darkgray',border ='white')
#正确的方法:
#x < - data()[,input $ xcol]
#bins < - nrow(data())
#hist(x,breaks = bins,col ='darkgray ',border ='white')
#I因为你有两个输入,我决定做一个scatterpl (x)
})
})$ b(b $< - data
$ b shinyApp(ui,server)
I'm new to R-shiny apps, My application is very simple. It has two tabs, in the first, I upload a file such as csv, then in the second tab, I choose the columns that will be plotted, My solution is scattered over several example each is not the same as mine, I want the uploaded dataset to be seen and can be used in all functions not just only when uploading it.
my server.R
library(shiny) shinyServer(function(input, output) { output$contents <- renderTable({ inFile <- input$file1 if (is.null(inFile)) return(NULL) read.csv(inFile$datapath, header=input$header, sep=input$sep, quote=input$quote) }) output$MyPlot <- renderPlot({ x <- contents()$contents[, c(input$xcol, input$ycol)] bins <- nrow(contents()) hist(x, breaks = bins, col = 'darkgray', border = 'white') }) })
ui.R
library(shiny) library(datasets) shinyUI(fluidPage( titlePanel("Column Plot"), tabsetPanel( tabPanel("Upload File", titlePanel("Uploading Files"), sidebarLayout( sidebarPanel( fileInput('file1', 'Choose CSV File', accept=c('text/csv', 'text/comma-separated-values,text/plain', '.csv')) ), mainPanel( tableOutput('contents') ) ) ), tabPanel("First Type", pageWithSidebar( headerPanel('My First Plot'), sidebarPanel( selectInput('xcol', 'X Variable', names(content)), selectInput('ycol', 'Y Variable', names(content), selected=names(content)[[2]]) ), mainPanel( plotOutput('MyPlot') ) ) ) ) ) )
I've tried to that, but I'm just starting that so, what should I do please?
解决方案You can create a reactive dataset (for instance
data
) in which your app reads the uploaded file and updates inputs - in this case names of a data frame and pass it to therender*
functions. I made some more detailed comments in the code.library(shiny) library(datasets) ui <- shinyUI(fluidPage( titlePanel("Column Plot"), tabsetPanel( tabPanel("Upload File", titlePanel("Uploading Files"), sidebarLayout( sidebarPanel( fileInput('file1', 'Choose CSV File', accept=c('text/csv', 'text/comma-separated-values,text/plain', '.csv')), # added interface for uploading data from # http://shiny.rstudio.com/gallery/file-upload.html tags$br(), checkboxInput('header', 'Header', TRUE), radioButtons('sep', 'Separator', c(Comma=',', Semicolon=';', Tab='\t'), ','), radioButtons('quote', 'Quote', c(None='', 'Double Quote'='"', 'Single Quote'="'"), '"') ), mainPanel( tableOutput('contents') ) ) ), tabPanel("First Type", pageWithSidebar( headerPanel('My First Plot'), sidebarPanel( # "Empty inputs" - they will be updated after the data is uploaded selectInput('xcol', 'X Variable', ""), selectInput('ycol', 'Y Variable', "", selected = "") ), mainPanel( plotOutput('MyPlot') ) ) ) ) ) ) server <- shinyServer(function(input, output, session) { # added "session" because updateSelectInput requires it data <- reactive({ req(input$file1) ## ?req # require that the input is available inFile <- input$file1 # tested with a following dataset: write.csv(mtcars, "mtcars.csv") # and write.csv(iris, "iris.csv") df <- read.csv(inFile$datapath, header = input$header, sep = input$sep, quote = input$quote) # Update inputs (you could create an observer with both updateSel...) # You can also constraint your choices. If you wanted select only numeric # variables you could set "choices = sapply(df, is.numeric)" # It depends on what do you want to do later on. updateSelectInput(session, inputId = 'xcol', label = 'X Variable', choices = names(df), selected = names(df)) updateSelectInput(session, inputId = 'ycol', label = 'Y Variable', choices = names(df), selected = names(df)[2]) return(df) }) output$contents <- renderTable({ data() }) output$MyPlot <- renderPlot({ # for a histogram: remove the second variable (it has to be numeric as well): # x <- data()[, c(input$xcol, input$ycol)] # bins <- nrow(data()) # hist(x, breaks = bins, col = 'darkgray', border = 'white') # Correct way: # x <- data()[, input$xcol] # bins <- nrow(data()) # hist(x, breaks = bins, col = 'darkgray', border = 'white') # I Since you have two inputs I decided to make a scatterplot x <- data()[, c(input$xcol, input$ycol)] plot(x) }) }) shinyApp(ui, server)
这篇关于如何绘制使用闪亮上传的数据集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!