对Shiny反应数据帧进行持久更改

时间:2015-07-31 12:28:51

标签: r shiny reactive-programming

我有反应数据框,我想根据两个用户输入更改变量名称。首先,用户从下拉列表中选择要更改的变量的名称。其次,用户在文本输入中输入变量的新名称,然后单击按钮进行更改。我已经用原始变量替换了文本输入但是当我去更改另一个变量时,数据帧没有被更新'所以以前的改变就丢失了。基本上我想对数据帧进行持久的更改。下面是一个示例代码。

server.R

#### Initiate shinyServer
shinyServer(function(input, output) {

### This reactive function will take the input "inFile" from the UI.R and store it as data
  inFile<-reactive({

    file1<-input$rawdata

    if(is.null(file1)) stop ("Please Upload a Valid .csv file")
    datas<-fread(file1$datapath, skip="Block") #### <<- set as datas as a variable outside the function

    datas<-as.data.frame(datas)

  assign('datas', datas, envir=parent.env(environment()) )
    return(datas)

  })

## Set the UI for selecting the chamber name to change

output$Chamber_select<-renderUI({
  if(is.null(inFile())) 
    return()
  ## Get the data set with the unique chamber Names
  isolate(named_Chamber<-unique(datas2$Chamber)

  ## Fill the selection box with the available chambers
  selectInput("Chamber_to_rename", 'Chambers to rename', c(Choose=0, named_Chamber), multiple=FALSE)
})


#### Another dataframe which is reactive, allow it to be subsetted and variable (chamber) renamed based upon user input

datas_edit<-reactive({

  datas2=inFile()

  if (input$change>0){


 isolate(c1<-input$Chamber_to_rename)
 isolate(c2<-input$Chamber_change)

 isolate(datas2$Chamber[datas2$Chamber==c1]<-c2)   ## The text input replaces the chamber which is selected ### HEREIN THE PROBLEM LIES, DOES NOT UPDATE

  }
  isolate(assign('datas2', datas2, envir=parent.env(environment()) ))

})

})

ui.R

shinyUI(fluidPage(


  tabsetPanel(
    id = 'dataset',
    tabPanel('Tabular', dataTableOutput('table')),
    tabPanel('Box Plot', plotOutput('box'))),


  fluidRow(
    column(4,
           h4("1.File Options:"),
           br(),

           fileInput("rawdata", "Enter your .csv file")
           ),

      column(4,
             uiOutput("Chamber_select"),
             textInput("Chamber_change", "Change Chamber name"),
             actionButton("change","Make Change")

      )


    ) #end fluidrow
  )  ### Fluid page end 
)    #### Shiny UI end

1 个答案:

答案 0 :(得分:1)

问题是在@NicE的指导下解决的。似乎存在!exists的问题所以is.null被使用了。以下代码已得到纠正。

#### Initiate shinyServer
shinyServer(function(input, output) {

### This reactive function will take the input "inFile" from the UI.R and store it as data
  inFile<-reactive({

    file1<-input$rawdata

    if(is.null(file1)) stop ("Please Upload a Valid .csv file")
    datas<-fread(file1$datapath, skip="Block") #### <<- set as datas as a variable outside the function

    datas<-as.data.frame(datas)

  assign('datas', datas, envir=parent.env(environment()) )
    return(datas)

  })

## Set the UI for selecting the chamber name to change

output$Chamber_select<-renderUI({
  if(is.null(inFile())) 
    return()
  ## Get the data set with the unique chamber Names
  isolate(named_Chamber<-unique(datas2$Chamber)

  ## Fill the selection box with the available chambers
  selectInput("Chamber_to_rename", 'Chambers to rename', c(Choose=0, named_Chamber), multiple=FALSE)
})


#### Another dataframe which is reactive, allow it to be subsetted and variable (chamber) renamed based upon user input

datas_edit<-reactive({

  if(is.null(datas2)){
       datas2<-inFile()
       return(datas2)
  }

  if (input$change>0){


 isolate(c1<-input$Chamber_to_rename)
 isolate(c2<-input$Chamber_change)

 isolate(datas2$Chamber[datas2$Chamber==c1]<-c2)   ## The text input replaces the chamber which is selected ### HEREIN THE PROBLEM LIES, DOES NOT UPDATE

  }
  isolate(assign('datas2', datas2, envir=parent.env(environment()) ))

})

})