2016-04-27 58 views
0

我是新来的闪亮和r。使用闪亮教程中的文件上传,我希望用户在应用程序会话中分配文件名,因为我可能会在同一个会话中加载其他文件。我不想结束会话并重新启动,也不想在代码中硬编码数据集分配。我还没有想出如何用无功输出来做到这一点。当我分配userInput $文件名并尝试加载表时,它只是给出userInput $文件名。我想知道这是否可能。闪亮的R文件上传为文件指定名称

因此,如果我加载mtcars.csv并且userInput $ filename是“cars”,那么我可以在其他选项卡中使用“cars”。 如果我然后用userInput $文件名“rocks”加载rocks.csv,我将能够在其他选项卡的userInput字段中使用“rocks”。

这也将使我能够使用userInput $文件名也许通过粘贴下载文件的名字了。

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) 

    }) 
}) 

回答

0

input$Filename表示对应textInput的值,所以可以不使用它来保存数据帧。你可以做的就是创建一个基于input$Filename一个动态命名的变量(也许应该重新命名为input$variable_name虽然。

assign(input$variable_name, dataset) 

,这将创建与您在输入框中输入的名称和作为值的变量从文件中读取数据集。

+0

谢谢我试试看。 – user6259251