簡體   English   中英

在Shiny中子集數據幀

[英]Subsetting Dataframe within Shiny

如果我使用fileInput從Shiny導入數據集,該如何以反應形式創建該數據集,從而可以創建導入數據fileInput子集並最終fileInput集數據fileInput不同行執行計算? 我可以將子集數據幀存儲為reactValues(),然后在反應場景之外使用它們嗎?

我將如何完成下面的代碼(在普通的R腳本中可以成功運行的代碼)?

df <- read.table(file.choose(), header=TRUE, sep=",")
attach(df)
df <- df[, c(1, 50:75)]
df[1] <- time

我知道我可以使用fileInput完成以下操作,我只是不確定如何在renderPlot這樣的事情子集化,並使它們在renderPlot等場景中可用。 reactivereactiveValues是實現這一目標的最佳策略嗎?

這是您要找的東西嗎?

library(shiny)
ui <- fluidPage(
  titlePanel("Uploading Files"),
  sidebarLayout(sidebarPanel(
    fileInput(
      "file1",
      "Choose CSV File",
      multiple = F,
      accept = c("text/csv", "text/comma-separated-values,text/plain", "text")
    ),
    uiOutput("selectbox")
  ),
  mainPanel(tableOutput("contents")))
  )

server <- function(input, output) {
  data <- reactive({
    req(input$file1)
    df <- read.csv(file = input$file1$datapath,
             header = T,
             sep = "\t")
  })

  output$selectbox <- renderUI({
    colnam <- colnames(data())
    selectInput("colsel",
                "Columns Selected",
                c("Please select" = "", colnam),
                multiple = T)
  })

  output$contents <- renderTable({
    data()[, c(req(input$colsel))]
  })
}

shinyApp(ui, server)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM