简体   繁体   English

闪亮的R文件上传为文件指定名称

[英]Shiny R file upload assign a name to the file

I am new to shiny and r. 我是Shiny和R的新手。 Using the file upload from the shiny tutorial, I want a user to assign the file name within the application session since I may load other files during the same session. 使用从闪亮教程中上传的文件,我希望用户在应用程序会话中分配文件名,因为我可能会在同一会话中加载其他文件。 I don't want to end the session and restart nor to hardcode the dataset assignments in the code. 我既不想结束会话并重新启动,也不想在代码中对数据集分配进行硬编码。 I haven't figured out how to do it with reactive output either. 我也没有弄清楚如何用无功输出。 When I assign the userInput$filename and try to load the table it just gives the userInput$filename. 当我分配userInput $ filename并尝试加载表时,它仅给出了userInput $ filename。 I wonder if this is possible. 我想知道这是否可能。

So if I load mtcars.csv and the userInput$filename is "cars" I'll be able to use that in other tabs with "cars". 因此,如果我加载mtcars.csv且userInput $ filename是“ cars”,则可以在其他选项卡中使用“ cars”。 If I then load rocks.csv with the userInput$filename of "rocks", I'll be able to use "rocks" in a userInput field in other tabs. 然后,如果我使用userInput $ filename的“ rocks”文件名加载rocks.csv,则可以在其他选项卡的userInput字段中使用“ rocks”。

This will also enable me to use the userInput$filename to perhaps via paste download the file with a name too. 这也将使我能够使用userInput $ filename来通过粘贴粘贴具有相同名称的文件。

ui.r
library(shiny)

shinyUI(fluidPage(
  titlePanel("Uploading Files"),
  sidebarLayout(
    sidebarPanel(
         textInput("Filename","Name of File for Session: ", ""),
         fileInput('file1', 'Choose CSV File',
          accept=c('text/csv', 
                             'text/comma-separated-values,text/plain', 
                             '.csv')),
  tags$hr(),
  checkboxInput('header', 'Header', TRUE),
  radioButtons('sep', 'Separator',
               c(Comma=',',
                 Semicolon=';',
                 Tab='\t'),
               ','),
  radioButtons('quote', 'Quote',
               c(None='',
                 'Double Quote'='"',
                 'Single Quote'="'"),
               '"')
),
mainPanel(
  tableOutput('contents')
 )
 )
))

server.R 服务器

library(shiny)

shinyServer(function(input, output) {
output$contents <- renderTable({

# input$file1 will be NULL initially. After the user selects
# and uploads a file, it will be a data frame with 'name',
# 'size', 'type', and 'datapath' columns. The 'datapath'
# column will contain the local filenames where the data can
# be found.

inFile <- input$file1

if (is.null(inFile))
  return(NULL)

dataset <- read.csv(inFile$datapath, header=input$header, sep=input$sep, 
             quote=input$quote)
## This is where I get stuck because I want the dataset to be input$Filename
## newdataset <- input$Filename

data.table(dataset)

  })
})

input$Filename represents the value of the corresponding textInput , so you cannot use it to hold the data frame. input$Filename表示相应的textInput的值,因此您不能使用它来保存数据框。 What you can do is to create a dynamically-named variable based on input$Filename (should probably rename it to input$variable_name though. 您可以做的是基于input$Filename创建一个动态命名的变量(不过应该将其重命名为input$variable_name

assign(input$variable_name, dataset)

which will create a variable with the name you entered in the input box and the value being the dataset read from file. 它将使用您在输入框中输入的名称创建一个变量,该值是从文件读取的数据集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM