2016-04-27 8 views
0

私は光沢があり、新しいです。光沢のあるチュートリアルのファイルアップロードを使用して、同じセッション中に他のファイルを読み込む可能性があるため、ユーザーはアプリケーションセッション内でファイル名を割り当てる必要があります。セッションを終了して再起動したり、コード内のデータセットの割り当てをハードコードしたりすることは望ましくありません。私はどのように反応出力でそれを行うかを理解していない。 userInput $ filenameを割り当ててテーブルをロードしようとすると、userInput $ filenameが返されます。これが可能かどうか疑問に思います。Shiny Rファイルのアップロードファイルに名前をつける

したがって、mtcars.csvを読み込み、userInput $ filenameが "cars"の場合、他のタブで "cars"を使用することができます。 次に、rocks.csvを "rocks"というuserInput $ファイル名でロードすると、他のタブのuserInputフィールドに "rocks"を使用できるようになります。

これにより、私は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) 

    }) 
}) 

答えて

0

input$Filename対応textInputの値を表すので、あなたは、データフレームを保持するためにそれを使用することはできません。あなたにできることは(おそらくしかしinput$variable_nameに名前を変更する必要がありinput$Filenameに基づいて動的に-という名前の変数を作成することです。あなたが入力ボックスとされた値に入力した名前を持つ変数を作成します

assign(input$variable_name, dataset) 

ファイルから読み取られたデータセット

+0

ありがとう私はそれを試してみましょう。 – user6259251

関連する問題