【问题标题】:Dataframe - R - Shiny数据框 - R - 闪亮
【发布时间】:2021-06-21 07:09:36
【问题描述】:

我对 Shiny 和数据框的使用有疑问。

我想我明白我需要创建隔离或反应式环境来与之交互,但是如果我尝试使用 Dataframe,我会收到一条错误消息:

Error in pfData: konnte Funktion "pfData" nicht finden

我试图通过这段代码来操作数据框:

server <- function(input, output) {
observeEvent(input$go,
           {
             pf_name <- reactive({input$pfID})
             pf_date <- reactive({input$pfDate})

             if (pf_name()!="please select a PF") {
               
               pfData <- reactive(read.csv(file =paste(pf_name(),".csv",sep=""),sep=";",dec=","))
               MDur <- pfData()[1,15]
               pfData <- pfData()[3:nrow(pfData()),]
               Total = sum(pfData()$Eco.Exp...Value.long)

               }
           })
}

如果我在控制台中操作我的数据框,它就可以正常工作:

pfData <- pfData[3:nrow(pfData),]
Total = sum(pfData$Eco.Exp...Value.long)
Assets = sum(as.numeric(gsub(",",".",gsub(".","",pfData$Value,fixed=TRUE),fixed=TRUE)))
pfData$Exposure <- with(pfData, Eco.Exp...Value.long /Total)

你能帮帮我吗?

编辑:

 library(shiny)

ui <- fluidPage(

  
fluidRow(
  
        column(6, offset =3,
               wellPanel(
                 "Choose Mandate and Date",
                         fluidRow(
                           column(4,selectInput("pfID",label = "",
                                                       choices = list("please select a PF","GF25", 
                                                                      "FPM"),
                                                       selected = "please select a PF") ), 
                                  column(4,   dateInput("pfDate",label="",value = Sys.Date())  ),
                           column(2,  actionButton("go","Submit")),column(2,textOutput("selected_var"))
                           )
                        )
              )
      )

)

# Define server logic ----
server <- function(input, output) {
  
  pfDataReactive <- reactive({
    input$go
    if (pf_name()!="please select a PF") {
      pfData <- read.csv(file =paste(pf_name(),".csv",sep=""),sep=";",dec=",")
      MDur <- pfData[1,15]
      pfData <- pfData[3:nrow(pfData),]
      Total = sum(pfData$Eco.Exp...Value.long)
      Assets = sum(as.numeric(gsub(",",".",gsub(".","",pfData$Value,fixed=TRUE),fixed=TRUE)))
      pfData$Exposure <- with(pfData, Eco.Exp...Value.long /Total)
      pfData
      output$selected_var <- renderText({paste(MDur)})
      }
  })
  
  

}

# Run the app ----
shinyApp(ui = ui, server = server)

谢谢 斯蒂芬

【问题讨论】:

    标签: r dataframe shiny


    【解决方案1】:

    如果没有工作示例,就不可能确定您要做什么,但听起来您需要reactive 而不是使用observeEvent

    试试类似的东西

    pfDataReactive <- reactive({
      input$go
      pfData <- read.csv(file =paste(pf_name(),".csv",sep=""),sep=";",dec=",")
      Total = sum(pfData$Eco.Exp...Value.long)
      Assets = sum(as.numeric(gsub(",",".",gsub(".","",pfData$Value,fixed=TRUE),fixed=TRUE)))
      pfData$Exposure <- with(pfData, Eco.Exp...Value.long /Total)
      pfData
    })
    

    然后在您的 Shiny 应用程序的服务器函数中使用 pfDataReactive(),无论您在控制台代码中引用 pfData 的任何位置。

    input$go 的独立引用可确保每当input$go 更改/被点击/等时,响应式都会更新。

    更新

    您的代码仍然存在重大问题。您已将分配添加到输出对象作为我给您的reactive 的最后一行,因此reactive 始终返回NULL。这没有帮助,也是它“根本不活跃”的原因之一......

    其次,当相关输入对象似乎是 input$pfID 时,您测试是否存在名为 pf_name 的反应/函数。这是reactive 从未更新的另一个原因。

    请注意我为提高pfDataReactive 对象的可读性而对input$pfID 的定义所做的更改。 (此更改也可能意味着您可以完全取消 input$go。)

    正如您所说,我无权访问您的 csv 文件,因此无法完全测试您的代码。我已经修改了pfDataReactive 的主体,以简单地将mtcars 数据集作为字符串返回。我还编辑了我已注释掉的代码,希望在您将其与 real csv 文件一起使用时能够正确运行。

    此代码似乎给出了您想要的行为。不过,如果我可以发表主观评论,我认为您的 GUI 布局令人震惊。 ;=)

    library(shiny)
    
    ui <- fluidPage(
      fluidRow(
        column(6, offset =3,
               wellPanel(
                 "Choose Mandate and Date",
                 fluidRow(
                   column(4,selectInput("pfID",label = "",
                                        # Modified to that "Pleaseselect a PF" returns NULL
                                        choices = list("please select a PF"="","GF25",  "FPM"),
                                        selected = "please select a PF") ), 
                   column(4,   dateInput("pfDate",label="",value = Sys.Date())  ),
                   column(2,  actionButton("go","Submit")),column(2,textOutput("selected_var"))
                 )
               )
        )
      )
    )
    
    # Define server logic ----
    server <- function(input, output) {
      pfDataReactive <- reactive({
        # Don't do anything until we have a PF csv file
        req(input$pfID)
        
        input$go
        # Note the change to the creation of the file name
          # pfData <- read.csv(file =paste(input$pfID,".csv",sep=""),sep=";",dec=",")
          # pfData <- pfData[3:nrow(pfData),]
          # Total = sum(pfData$Eco.Exp...Value.long)
          # Assets = sum(as.numeric(gsub(",",".",gsub(".","",pfData$Value,fixed=TRUE),fixed=TRUE)))
          # pfData$Exposure <- with(pfData, Eco.Exp...Value.long /Total)
          # MDur <- pfData[1,15]
          # If you want to print MDur in the selected_var output, MDur should be the retrun value from this reactive
          # MDur
          mtcars
      })
      
      output$selected_var <- renderText({
        print("Yep!")
        as.character(pfDataReactive())
      })
    }
    
    # Run the app ----
    shinyApp(ui = ui, server = server)
    

    下一次,拜托,拜托,多努力提供一个MWE。 This post 可能会有所帮助。

    This 是对 Shiny 的一个很好的介绍。

    【讨论】:

    • 嗨 Limey,感谢您的帮助。尝试了你的方式,但似乎它根本不活跃。由于您错过了 csv,因此很难有一个有效的示例,但这将是我的完整代码。请参阅上面的编辑文本!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-06
    • 2018-09-26
    • 2017-11-15
    • 2013-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多