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

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

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

  1. library(shiny)
  2. ui <- fluidPage(titlePanel("daily data manipulation"),
  3. sidebarLayout(sidebarPanel(
  4. fileInput('file1', 'Choose xlsx file',
  5. accept = c(".xlsx"))
  6. ),
  7. mainPanel(tableOutput(outputId = "mytable")
  8. )))

server.r

  1. server <- function(input, output) {
  2. data <- reactive({
  3. inFile <- input$file1
  4. if(!is.null(inFile)) {
  5. readxl::read_excel(inFile$datapath)
  6. }
  7. })
  8. dat <- data()
  9. output$mytable <- renderTable({
  10. dat$newcolumn <- 3
  11. })
  12. }
英文:

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

  1. library(shiny)
  2. ui &lt;- fluidPage(titlePanel(&quot;daily data manipulation&quot;),
  3. sidebarLayout(sidebarPanel(
  4. fileInput(&#39;file1&#39;, &#39;Choose xlsx file&#39;,
  5. accept = c(&quot;.xlsx&quot;))
  6. ),
  7. mainPanel(tableOutput(outputId = &quot;mytable&quot;)
  8. )))

server.r

  1. server &lt;- function(input, output) {
  2. data &lt;- reactive({
  3. inFile &lt;- input$file1
  4. if(!is.null(inFile)) {
  5. readxl::read_excel(inFile$datapath)
  6. }
  7. })
  8. dat &lt;- data()
  9. output$mytable &lt;- renderTable({
  10. dat$newcolumn &lt;- 3
  11. })
  12. }

答案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.

  1. server &lt;- function(input, output, session) {
  2. data &lt;- reactive({
  3. inFile &lt;- input$file1
  4. if(!is.null(inFile)) {
  5. readxl::read_excel(inFile$datapath)
  6. }
  7. })
  8. output$mytable &lt;- renderTable({
  9. dat &lt;- data()
  10. if(!is.null(dat))
  11. {
  12. dat$newcolumn &lt;- 3
  13. dat
  14. }
  15. })
  16. }

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:

确定