2017-06-18 3 views
1

私は新しい変数を作成し、それを既存のデータフレームに追加したいと思います。ユーザーはtextInputを使用して名前と定義を入力する必要があります。注:私はすべての提案を投稿しましたGetting strings recognized as variable names in RユーザーがtextInputを使用して新しい変数名と定義を指定させる

まだ、私はそれを動作させることができません。どんな提案も高く評価されます。ありがとうございました!ここで

私のコードです:

生データ:

colA <- c('1','2','3','3','2') 
colB <- c('1','1','3','3','2') 
colC <- c('14','12','33','33','26') 
colD <- c('Value','Mainstream','Value','Premium','Premium') 
colE <- c(1,2,3,4,5) 
rawdata <- as.data.frame(cbind(colA, colB, colC, colD, colE)) 

ui.R:

fluidPage(
      sidebarLayout(
       sidebarPanel(
        textInput("NewVar_Name", "New attribute's name"), 
        textInput("NewVar_Def", "New attribute's definition", 
          "ifelse(rawdata$price_tiers_new == 'Value', 1, 0)"), 

        br(), 
        actionButton("addButton", strong("Done!")), 
        width = 3 
       ), 

       mainPanel(
        verticalLayout(
         br() 
         #Will display a summary of new variable 
        ) 
       ) 
      ) 
     ) 

server.R:

function(input, output, session) { 

    temp <- reactiveValues() 

    observe(
     if(input$addButton == 0) { 
      temp$df <- rawdata 
     } else { 
      temp$df <- mydata() 
     } 
    ) 


    mydata <- eventReactive(input$addButton, { 
     if(input$addButton == 0) { 
      rawdata 
     } else { 
      tempname <- eval(as.name(input$NewVar_Name)) 
      do.call("<-",list(tempname, eval(parse(text=input$NewVar_Def)))) 

      cbind(temp$df, tempname) 
     } 
    }) 

} 
+0

これはあなたのために働く必要があります。https://stackoverflow.com/questions/44598544/allow-users-to-add-new-variables-to-a-dataset-in-shiny-app/44599073#44599073 – BigDataScientist

+0

こと新しい変数を追加するために完全に動作します(ありがとう!)。しかし、この例では、ユーザーから変数名を取得しません。 – Ketty

答えて

0

私はあなたが

を行うことができると思います
mydata <- eventReactive(input$addButton, { 
    if(input$addButton == 0) { 
     rawdata 
    } else { 
     tempdata <- eval(parse(text=input$NewVar_Def)) 
     temp$df <- setNames(cbind(
     temp$df, 
     tempdata), 
     c(names(temp$df), input$NewVar_Name)) 
    } 
}) 

データセットにprice_tiers_newという名前の列がないため、ifelse(rawdata$price_tiers_new == 'Value', 1, 0)は機能しません。

+0

あなたは正しいです。 Price_tiers_newは他のデータセットでも使用できるので、この場合は動作しません。しかし、あなたの提案は完全に機能します。ありがとうございました! – Ketty

関連する問題