简体   繁体   中英

Dataframe - R - Shiny

i have a question regarding Shiny and the usage of Data frames.

I think i understood that i need to create isolated or reactive environmentes to interact with, but if i try to work with the Dataframe i get an error message:

Error in pfData: konnte Funktion "pfData" nicht finden

i tried to manipulate the dataframe by this code:

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)

               }
           })
}

If i manipulate my Dataframe in the console it works just fine:

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)

can you help me?

Edit:

 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)

Thank you Stefan

Without a working example, it's imposible to be sure what you're trying to do, but it sounds like you need a reactive rather than using observeEvent .

Try something like

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
})

And then use pfDataReactive() in your Shiny app's server function wherever you would refer to pfData in your console code.

The standalone reference to input$go ensures the reactive will update whenever input$go changes/is clicked/etc.

Update

There are still significant issues with your code. You've added an assignment to an output object as the last line of the reactive I gave you, so the reactive always returns NULL . That's not helpful and is one of the reasons why it "doesn't active at all"...

Second, you test for the existence of an reactive/function called pf_name when the relevant input object appears to be input$pfID . That's another reason why the reactive is never updated.

Note the change to the definition of input$pfID that I've made to improve the readability of the pfDataReactive object. (This change also probably means that you can do away with input$go entirely.)

As you say, I don't have access to your csv file, so I can't test your code completely. I've modified the body of the pfDataReactive to simply return the mtcars dataset as a string. I've also edited the code I've commented out to hopefully run correctly when you use it with the real csv file.

This code appears to give the behaviour you want,. Though, if I may make a subjective comment, I think the layout of your GUI is appaling. ;=)

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)

Next time, please, please, make more effort to provide a MWE. This post may help.

This is a good introduction to Shiny.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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