繁体   English   中英

Dataframe - R - Shiny

[英]Dataframe - R - Shiny

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

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

Error in pfData: konnte Funktion "pfData" nicht finden

我试图通过以下代码操纵 dataframe:

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)

               }
           })
}

如果我在控制台中操作我的 Dataframe 它工作得很好:

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)

谢谢斯特凡

如果没有一个工作示例,就不可能确定您要做什么,但听起来您需要一个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 应用程序的服务器 function 中使用pfDataReactive()在控制台代码中引用pfData的任何地方。

input$go的独立引用确保每当input$go更改/被单击/等时响应式将更新。

更新

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

其次,当相关输入 object 似乎是input$pfID时,您测试是否存在名为pf_name的反应/函数。 这就是为什么reactive永远不会更新的另一个原因。

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

正如你所说,我无权访问你的 csv 文件,所以我无法完全测试你的代码。 我已经修改了pfDataReactive的主体,以简单地将mtcars数据集作为字符串返回。 我还编辑了我注释掉的代码,希望当您将它与真正的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。 这篇文章可能会有所帮助。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM