如何在用户选择要在R上载文件时指定列? [英] How to Specify Columns when a user chooses a file to upload in R?

查看:196
本文介绍了如何在用户选择要在R上载文件时指定列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个 R 文件,它提示用户上传文件并在用户上传的文件中绘制数据。我不知道如何在我的代码中引用这些列(我试图使用ggplot2)。



用户上传的数据将是一个CSV文件,看起来像,但可以有所不同:

  1月2月3月4月5月
汉堡包4 5 3 5 2

我被困在ggplot2部分,我需要引用列名称。

server.R

 库(闪亮)
库(数据集)
库(ggplot2)

X < - read.csv(file.choose())


#定义服务器逻辑(输入,输出){


#生成数据集的摘要
输出$摘要< - 要求汇总和查看选定的数据集
shinyServer renderPrint({
dataset< - X
summary(dataset)
})

#显示第一个n个观测值
输出值$ view< ; - renderTable({
head(X,n = input $ obs)
})

#创建线条图(我从https://gist.github中获取了该图。 (x,y = count,group = name,color = name)
output $ plot <-activePlot(function(){
print(ggplot(X,aes )+
geom_line()+ ylab()+ xlab()+ theme_bw()+
主题(legend.position =top,legend.title = element_blank(),legend。 text = element_text(color =blue,size = 14,face =bold)))

))
})

UI.r



 库(闪亮)

#定义数据集查看器应用程序的UI
shinyUI(pageWithSidebar(

#应用程序标题
headerPanel(Sample Proj),

#带有控件的侧栏,用于选择数据集并指定数字
观察值查看
sidebarPanel(
numericInput(obs,观察数量:,10)

),

#显示一个摘要数据集和一个HTML表格,其中包含请求的
#个观察值
mainPanel(
tabsetPanel(
tabPanel(Table,tableOutput(view)),
tabPanel(LineGraph,plotOutput(plot))


))


解决方案

下面是一个工作示例。我把你的代码和修改,以便列号可以从 UI.R 作为输入传递。 (我使用ggplot2中的钻石数据集作为我的数据框。)



请注意,我创建了一对 reactive 函数在Server.R中。



Server.R



  library(闪亮)
库(数据集)
库(ggplot2)

#x < - read.csv(file.choose())
x < - diamonds

#定义汇总和查看选定数据集所需的服务器逻辑
shinyServer(function(input,output){

createPlot< - function(df,colx, coly] {
x< - names(df)[colx]
y< - names(df)[coly]
ggplot(data = df,aes_string(x = x,y = y ))+ geom_line()
}

Y < - reactive({
x
})

#生成数据集
output $ summary< - renderPrint({
dataset< - x
summary(dataset)
})

#显示第一个 n观察
输出$ view< - renderTable({
head(x,n = input $ obs)
})

#创建线图(我从https://gist.github.com/pssguy/4171750获得了这个)
output $ plot < - reactivePlot(function(){$ b $ (df,colx = input $ xa,coly = input $ ya))
})
})



UI.R



 图书馆(闪亮)

#为数据集查看器应用程序定义UI
shinyUI(pageWithSidebar(

#应用程序标题
headerPanel(Sample Proj),

#侧边栏包含用于选择数据集的控件,并指定数字
观察值以查看
sidebarPanel(
numericInput(obs,view to view to view :,10)
,numericInput(xa,绘制为X轴的列:,5)
,numericInput(ya,要绘制为Y轴的列: ,6)

),

#显示数据集和一个HTML表的摘要,其中包含所需的
#个观察值
mainPanel(
tabsetPanel(
tabPanel(Table, tableOutput(view)),
tabPanel(LineGraph,plotOutput(plot))


))
file.choose ()
选项与变量数据框。



希望这可以帮助你前进。



< h3>根据@ joran的评论更新:

我最初的回应是使用ggplot的 aes 中的列号,添加了 environment = environment()参数。我修改了server.R中的createPlot函数,使用 aes_string 代替。


I am writing an R file which prompts a user to upload a file and plots the data in the file the user uploads. I do not know how to reference the columns however (I am trying to use ggplot2) in my code.

The data the user will upload will be a CSV file that would look something like, but can vary:

        January February March April May
Burgers    4       5       3     5    2

I am stuck at the ggplot2 part where I need to reference column names.

server.R

library(shiny)
library(datasets)
library(ggplot2)

X <- read.csv(file.choose())


# Define server logic required to summarize and view the selected dataset
shinyServer(function(input, output) {


  # Generate a summary of the dataset
  output$summary <- renderPrint({
    dataset <- X
    summary(dataset)
  })

  # Show the first "n" observations
  output$view <- renderTable({
    head(X, n = input$obs)
  })

  # create line plot (I took this from https://gist.github.com/pssguy/4171750)
  output$plot <- reactivePlot(function() {
      print(ggplot(X, aes(x=date,y=count,group=name,colour=name))+
              geom_line()+ylab("")+xlab("") +theme_bw() + 
              theme(legend.position="top",legend.title=element_blank(),legend.text = element_text(colour="blue", size = 14, face = "bold")))

  })
})

UI.r

library(shiny)

# Define UI for dataset viewer application
shinyUI(pageWithSidebar(

  # Application title
  headerPanel("Sample Proj"),

  # Sidebar with controls to select a dataset and specify the number
  # of observations to view
  sidebarPanel(
    numericInput("obs", "Number of observations to view:", 10)

  ),

  # Show a summary of the dataset and an HTML table with the requested
  # number of observations
  mainPanel(
    tabsetPanel(
      tabPanel("Table", tableOutput("view")),
      tabPanel("LineGraph", plotOutput("plot"))
    )
  )
))

解决方案

Here's a working example. I took your code and modified it so that the Column Numbers can be passed from UI.R as inputs. (I use the diamonds dataset in ggplot2 for my dataframe.)

Note that I have created a couple of reactive functions in Server.R.

Server.R

library(shiny)
library(datasets)
library(ggplot2)

#x <- read.csv(file.choose())
x <- diamonds

# Define server logic required to summarize and view the selected dataset
shinyServer(function(input, output) {

  createPlot <- function(df, colx, coly) {
    x <- names(df)[colx] 
    y <- names(df)[coly] 
    ggplot(data=df, aes_string(x = x, y = y) ) + geom_line()
  }

  Y <- reactive({
    x
  })

  # Generate a summary of the dataset
  output$summary <- renderPrint({
    dataset <- x
    summary(dataset)
  })

  # Show the first "n" observations
  output$view <- renderTable({
    head(x, n = input$obs)
  })

  # create line plot (I took this from https://gist.github.com/pssguy/4171750)
  output$plot <- reactivePlot(function() {
    df <- Y()
    print(createPlot(df, colx=input$xa, coly=input$ya))
  })
})

UI.R

library(shiny)

# Define UI for dataset viewer application
shinyUI(pageWithSidebar(

  # Application title
  headerPanel("Sample Proj"),

  # Sidebar with controls to select a dataset and specify the number
  # of observations to view
  sidebarPanel(
    numericInput("obs", "Number of observations to view:", 10)
    ,numericInput("xa", "Column to plot as X-axis:", 5)
    ,numericInput("ya", "Column to plot as Y-axis:", 6)

  ),

  # Show a summary of the dataset and an HTML table with the requested
  # number of observations
  mainPanel(
    tabsetPanel(
      tabPanel("Table", tableOutput("view")),
      tabPanel("LineGraph", plotOutput("plot"))
    )
  )
))

As a separate suggestion, you could first get your shiny app working with a static dataframe, then try the file.choose() option with variable data frames.

Hope this helps you move forward.

Updated based on @joran's comment:

My original response was using the column number inside ggplot's aes with an environment=environment() argument added. I have modified the createPlot function in server.R to use aes_string instead.

这篇关于如何在用户选择要在R上载文件时指定列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆