数据操纵在文件(Excel)输入后在Shiny中。

huangapple go评论74阅读模式
英文:

data manipulation after file (excel) input in shiny

问题

I have an Excel dataset that I want to manipulate after uploading it. Let's say in this case I would like to add a newcolumn <- 3, how can I get around with this? many thanks in advance.

ui.r

library(shiny)

ui <- fluidPage(titlePanel("daily data manipulation"),
                sidebarLayout(sidebarPanel(
                  fileInput('file1', 'Choose xlsx file',
                            accept = c(".xlsx"))
                ),
                mainPanel(tableOutput(outputId = "mytable")
                )))

server.r

server <- function(input, output) {

  data <- reactive({
    inFile <- input$file1
    if(!is.null(inFile)) {
      readxl::read_excel(inFile$datapath)   
    }
  })

  dat <- data()

  output$mytable <- renderTable({
    dat$newcolumn <- 3
  })

}
英文:

I have an Excel dataset that I want to manipulate after uploading it. Let's say in this case I would like to add a newcolumn &lt;- 3, how can I get around with this? many thanks in advance.

ui.r

    library(shiny)


ui &lt;- fluidPage(titlePanel(&quot;daily data manipulation&quot;),
                sidebarLayout(sidebarPanel(
                  fileInput(&#39;file1&#39;, &#39;Choose xlsx file&#39;,
                            accept = c(&quot;.xlsx&quot;))
                ),
                
                
                
                mainPanel(tableOutput(outputId = &quot;mytable&quot;) 
                  
                  )))

server.r

server &lt;- function(input, output) {
  
  

  data &lt;- reactive({
    inFile &lt;- input$file1
    if(!is.null(inFile)) {
      readxl::read_excel(inFile$datapath)   
    }
  })
  

 dat &lt;- data()
 

 
 output$mytable &lt;- renderTable({
   dat$newcolumn &lt;- 3
   
 })
 
  }

答案1

得分: 1

Reactive expressions can only be consumed in a reactive context, so dat &lt;- data() should be moved inside renderTable.

此外,在添加新列之前检查反应性值是否为null可以确保在用户上传Excel文件之前不会呈现新列。

英文:

Reactive expressions can only be consumed in a reactive context, so dat &lt;- data() should be moved inside renderTable.

server &lt;- function(input, output, session) { 
  data &lt;- reactive({
    inFile &lt;- input$file1
    if(!is.null(inFile)) {
      readxl::read_excel(inFile$datapath)
    }
  })

  output$mytable &lt;- renderTable({
    dat &lt;- data()
    if(!is.null(dat))
    {
      dat$newcolumn &lt;- 3
      dat
    }
  }) 
}

Also, checking if the reactive value is null before adding the new column ensures that until the user uploads an excel file the new column will not be rendered.

huangapple
  • 本文由 发表于 2023年5月21日 04:04:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76297141.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定